version 1.11, 2000/01/11 19:42:27
|
version 1.12, 2000/01/27 16:51:12
|
Line 92 typedef struct xmlNanoHTTPCtxt {
|
Line 92 typedef struct xmlNanoHTTPCtxt {
|
char *location; /* the new URL in case of redirect */ |
char *location; /* the new URL in case of redirect */ |
} xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr; |
} xmlNanoHTTPCtxt, *xmlNanoHTTPCtxtPtr; |
|
|
|
static int initialized = 0; |
|
static char *proxy = NULL; /* the proxy name if any */ |
|
static int proxyPort; /* the proxy port if any */ |
|
|
|
/** |
|
* xmlNanoHTTPInit: |
|
* |
|
* Initialize the HTTP protocol layer. |
|
* Currently it just checks for proxy informations |
|
*/ |
|
|
|
void |
|
xmlNanoHTTPInit(void) { |
|
const char *env; |
|
|
|
if (initialized) |
|
return; |
|
|
|
proxyPort = 80; |
|
env = getenv("no_proxy"); |
|
if (env != NULL) |
|
return; |
|
env = getenv("http_proxy"); |
|
if (env != NULL) { |
|
xmlNanoHTTPScanProxy(env); |
|
return; |
|
} |
|
env = getenv("HTTP_PROXY"); |
|
if (env != NULL) { |
|
xmlNanoHTTPScanProxy(env); |
|
return; |
|
} |
|
initialized = 1; |
|
} |
|
|
|
/** |
|
* xmlNanoHTTPClenup: |
|
* |
|
* Cleanup the HTTP protocol layer. |
|
*/ |
|
|
|
void |
|
xmlNanoHTTPCleanup(void) { |
|
if (proxy != NULL) |
|
xmlFree(proxy); |
|
initialized = 0; |
|
return; |
|
} |
|
|
/** |
/** |
* xmlNanoHTTPScanURL: |
* xmlNanoHTTPScanURL: |
* @ctxt: an HTTP context |
* @ctxt: an HTTP context |
Line 120 xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ct
|
Line 169 xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ct
|
xmlFree(ctxt->path); |
xmlFree(ctxt->path); |
ctxt->path = NULL; |
ctxt->path = NULL; |
} |
} |
|
if (URL == NULL) return; |
buf[index] = 0; |
buf[index] = 0; |
while (*cur != 0) { |
while (*cur != 0) { |
if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) { |
if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) { |
Line 173 xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ct
|
Line 223 xmlNanoHTTPScanURL(xmlNanoHTTPCtxtPtr ct
|
} |
} |
|
|
/** |
/** |
|
* xmlNanoHTTPScanProxy: |
|
* @URL: The proxy URL used to initialize the proxy context |
|
* |
|
* (Re)Initialize the HTTP Proxy context by parsing the URL and finding |
|
* the protocol host port it indicates. |
|
* Should be like http://myproxy/ or http://myproxy:3128/ |
|
* A NULL URL cleans up proxy informations. |
|
*/ |
|
|
|
void |
|
xmlNanoHTTPScanProxy(const char *URL) { |
|
const char *cur = URL; |
|
char buf[4096]; |
|
int index = 0; |
|
int port = 0; |
|
|
|
if (proxy != NULL) { |
|
xmlFree(proxy); |
|
proxy = NULL; |
|
} |
|
if (proxyPort != 0) { |
|
proxyPort = 0; |
|
} |
|
#ifdef DEBUG_HTTP |
|
if (URL == NULL) |
|
printf("Removing HTTP proxy info\n"); |
|
else |
|
printf("Using HTTP proxy %s\n", URL); |
|
#endif |
|
if (URL == NULL) return; |
|
buf[index] = 0; |
|
while (*cur != 0) { |
|
if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) { |
|
buf[index] = 0; |
|
index = 0; |
|
cur += 3; |
|
break; |
|
} |
|
buf[index++] = *cur++; |
|
} |
|
if (*cur == 0) return; |
|
|
|
buf[index] = 0; |
|
while (1) { |
|
if (cur[0] == ':') { |
|
buf[index] = 0; |
|
proxy = xmlMemStrdup(buf); |
|
index = 0; |
|
cur += 1; |
|
while ((*cur >= '0') && (*cur <= '9')) { |
|
port *= 10; |
|
port += *cur - '0'; |
|
cur++; |
|
} |
|
if (port != 0) proxyPort = port; |
|
while ((cur[0] != '/') && (*cur != 0)) |
|
cur++; |
|
break; |
|
} |
|
if ((*cur == '/') || (*cur == 0)) { |
|
buf[index] = 0; |
|
proxy = xmlMemStrdup(buf); |
|
index = 0; |
|
break; |
|
} |
|
buf[index++] = *cur++; |
|
} |
|
} |
|
|
|
/** |
* xmlNanoHTTPNewCtxt: |
* xmlNanoHTTPNewCtxt: |
* @URL: The URL used to initialize the context |
* @URL: The URL used to initialize the context |
* |
* |
Line 598 xmlNanoHTTPOpen(const char *URL, char **
|
Line 718 xmlNanoHTTPOpen(const char *URL, char **
|
int nbRedirects = 0; |
int nbRedirects = 0; |
char *redirURL = NULL; |
char *redirURL = NULL; |
|
|
|
xmlNanoHTTPInit(); |
if (contentType != NULL) *contentType = NULL; |
if (contentType != NULL) *contentType = NULL; |
|
|
retry: |
retry: |
Line 618 retry:
|
Line 739 retry:
|
xmlNanoHTTPFreeCtxt(ctxt); |
xmlNanoHTTPFreeCtxt(ctxt); |
return(NULL); |
return(NULL); |
} |
} |
ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port); |
if (proxy) |
|
ret = xmlNanoHTTPConnectHost(proxy, proxyPort); |
|
else |
|
ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port); |
if (ret < 0) { |
if (ret < 0) { |
xmlNanoHTTPFreeCtxt(ctxt); |
xmlNanoHTTPFreeCtxt(ctxt); |
return(NULL); |
return(NULL); |
} |
} |
ctxt->fd = ret; |
ctxt->fd = ret; |
|
if (proxy) { |
|
#ifdef have_snprintf |
|
if (ctxt->port != 80) |
|
snprintf(buf, sizeof(buf), |
|
"GET http://%s:%d%s HTTP/1.0\r\nHost: %s\r\n\r\n", |
|
ctxt->hostname, ctxt->port, ctxt->path, ctxt->hostname); |
|
else |
|
snprintf(buf, sizeof(buf),"GET http://%s%s HTTP/1.0\r\nHost: %s\r\n\r\n", |
|
ctxt->hostname, ctxt->path, ctxt->hostname); |
|
#else |
|
if (ctxt->port != 80) |
|
sprintf(buf, |
|
"GET http://%s:%d%s HTTP/1.0\r\nHost: %s\r\n\r\n", |
|
ctxt->hostname, ctxt->port, ctxt->path, ctxt->hostname); |
|
else |
|
sprintf(buf, "GET http://%s%s HTTP/1.0\r\nHost: %s\r\n\r\n", |
|
ctxt->hostname, ctxt->path, ctxt->hostname); |
|
#endif |
|
#ifdef DEBUG_HTTP |
|
if (ctxt->port != 80) |
|
printf("-> Proxy GET http://%s:%d%s HTTP/1.0\n-> Host: %s\n\n", |
|
ctxt->hostname, ctxt->port, ctxt->path, ctxt->hostname); |
|
else |
|
printf("-> Proxy GET http://%s%s HTTP/1.0\n-> Host: %s\n\n", |
|
ctxt->hostname, ctxt->path, ctxt->hostname); |
|
#endif |
|
} else { |
#ifdef HAVE_SNPRINTF |
#ifdef HAVE_SNPRINTF |
snprintf(buf, sizeof(buf),"GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", |
snprintf(buf, sizeof(buf),"GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", |
ctxt->path, ctxt->hostname); |
ctxt->path, ctxt->hostname); |
#else |
#else |
sprintf(buf, "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", |
sprintf(buf, "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", |
ctxt->path, ctxt->hostname); |
ctxt->path, ctxt->hostname); |
#endif |
#endif |
#ifdef DEBUG_HTTP |
#ifdef DEBUG_HTTP |
printf("-> GET %s HTTP/1.0\n-> Host: %s\n\n", |
printf("-> GET %s HTTP/1.0\n-> Host: %s\n\n", |
ctxt->path, ctxt->hostname); |
ctxt->path, ctxt->hostname); |
#endif |
#endif |
|
} |
ctxt->outptr = ctxt->out = xmlMemStrdup(buf); |
ctxt->outptr = ctxt->out = xmlMemStrdup(buf); |
ctxt->state = XML_NANO_HTTP_WRITE; |
ctxt->state = XML_NANO_HTTP_WRITE; |
xmlNanoHTTPSend(ctxt); |
xmlNanoHTTPSend(ctxt); |
Line 645 retry:
|
Line 797 retry:
|
if (head && (*p == 0)) { |
if (head && (*p == 0)) { |
head = 0; |
head = 0; |
ctxt->content = ctxt->inrptr; |
ctxt->content = ctxt->inrptr; |
|
xmlFree(p); |
break; |
break; |
} |
} |
xmlNanoHTTPScanAnswer(ctxt, p); |
xmlNanoHTTPScanAnswer(ctxt, p); |
Line 1068 int main(int argc, char **argv) {
|
Line 1221 int main(int argc, char **argv) {
|
printf("%s: minimal HTTP GET implementation\n", argv[0]); |
printf("%s: minimal HTTP GET implementation\n", argv[0]); |
printf("\tusage %s [ URL [ filename ] ]\n", argv[0]); |
printf("\tusage %s [ URL [ filename ] ]\n", argv[0]); |
} |
} |
|
xmlNanoHTTPCleanup(); |
|
xmlMemoryDump(); |
return(0); |
return(0); |
} |
} |
#endif /* STANDALONE */ |
#endif /* STANDALONE */ |