/*
 * nanohttp-select-bug.c
 *
 *  Created on: Sep 28, 2008
 *      Author: ericb
 */

#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <string.h>
#include <libxml/nanohttp.h>

#define NUM_FDS_TO_OPEN 90000

int main() {
	void *http_ctx;
	char buf[256];
	int i;

	memset(buf, 0, sizeof(buf));
	xmlNanoHTTPInit();

	/* Open up a bunch of file descriptors (more than 1024). */
	for (i = 0; i < NUM_FDS_TO_OPEN; i++) {
		socket(PF_INET, SOCK_STREAM, 0);
	}

	http_ctx = xmlNanoHTTPOpen("http://www.google.com/", NULL);
	if (http_ctx == NULL) {
		fprintf(stderr, "Couldn't open URL.\n");
		exit(1);
	}

	if (xmlNanoHTTPRead(http_ctx, buf, sizeof(buf) - 1) < 0) {
		fprintf(stderr, "Error reading URL.\n");
		exit(1);
	}

	printf("First 255 bytes of URL were:\n%s", buf);

	xmlNanoHTTPClose(http_ctx);
	xmlNanoHTTPCleanup();

	return 0;
}
