Show page source of libxml/nanohttp #40442

= NanoHTTPを使ったページ取得の例

この例では、環境変数http_proxyから、プロキシのURLを取得し、目的のファイルを取得する。(ただし、プロキシURLの取得部分は安全ではない。あくまで例である。)


{{{ code c
#include <stdio.h>
#include <stdlib.h>
#include <libxml/nanohttp.h>

void init_proxy(void)
{
	int length;
	char *proxy;
	char *proxy_url;

	if((proxy = getenv("http_proxy")) != NULL)
	{
		length = strlen(proxy) + strlen("http:///") + 1;
		proxy_url = malloc(sizeof(char)*length);
		sprintf(proxy_url, "http://%s/", proxy);

		xmlNanoHTTPScanProxy(proxy_url);
		free(proxy_url);
	}
}

int main(int argc, char **argv)
{
	char *content_type;

	/* Initialize NanoHTTP */
	init_proxy();
	xmlNanoHTTPInit();

	if(xmlNanoHTTPFetch("http://www10.plala.or.jp/always/", "tmp.html", &content_type) != -1)
	{
		puts(content_type);
		free(content_type);
	}

	/* Clean up NanoHTTP */
	xmlNanoHTTPCleanup();


	return EXIT_SUCCESS;
}
}}}