static int logResponse(FD *f, Buf *buf) { App *app; HTTP *http; if ((table[fileno(f->logFile)]->suspend) || (f->suspend)) { return 0; } fprintf ( f->logFile, "<script>\n" "w%d.document.write(\"" "<h3>Server Response</h3>" "<pre><b>", f->id ); http = httpAlloc(); http->in = buf; app = proxyApp(f); httpParseStream(http, app, (unsigned char *) "readProxyResponse"); free(app); httpFree(http); fprintf(f->logFile, "</b></pre>\");\n</script>\n"); fflush(f->logFile); return 1; }
HTTP * httpProcess(App *app, URL *url, char *version, HTTPNameValue *headers) { HTTP *http; int port; int sock; port = -1; if (url->port == -1) { port = 80; } else { port = url->port; } if (!url->host) { fprintf(stderr, "url->host is NULL for %s\n", url->url ? (char *) url->url : "<NULL>"); return NULL; } sock = netConnect(app, url->host, port); if (sock == -1) { return NULL; } http = httpAlloc(); http->url = url; if (version) { http->version = (unsigned char *) version; } else { http->version = (unsigned char *) "HTTP/1.0"; } http->headers = headers; httpGetObject(app, http, sock); close(sock); return http; }
static int logRequest(FD *f, Buf *buf) { App *app; HTTP *http; if ( (table[fileno(f->logFile)]->suspend) || (strstr((char *) current(buf), suspendStr)) ) { table[f->writeFD]->suspend = 1; return 0; } table[f->writeFD]->suspend = 0; fprintf ( f->logFile, "<script>\n" "w%d = window.open(\"\", \"%d\", \"scrollbars\");\n" "w%d.document.write(\"" "<h3>Client Request</h3>" "<pre><b>", f->id, f->id, f->id ); http = httpAlloc(); http->in = buf; app = proxyApp(f); httpParseRequest(http, app, "logRequest"); free(app); httpFree(http); fprintf(f->logFile, "</b></pre>\");\n</script>\n"); fflush(f->logFile); return 1; }
/** * HTTP server thread * @param host IP address of interface on which to listen * @param gentcpdev the allocated tcp device for general listening * @return OK or SYSERR */ thread httpServer(int netDescrp, int gentcpdev) { tid_typ shelltid, killtid; int tcpdev, httpdev; char thrname[TNMLEN]; bool noGenHTTP; struct netaddr *host; struct netif *nif; enable(); noGenHTTP = FALSE; wait(maxhttp); /* Make sure max HTTP threads not reached */ /* Allocate HTTP device */ httpdev = httpAlloc(); /* Received bad http device, close out allocated resources */ if (isbadhttp(httpdev)) { printf("failed to allocate proper HTTP device\n"); signal(maxhttp); return SYSERR; } /* Back up the general tcpdev number */ tcpdev = gentcpdev; /* Look up the network descriptor */ nif = netLookup(netDescrp); if (SYSERR == (int)nif) { fprintf(stderr, "%s is not associated with an active network", devtab[netDescrp].name); fprintf(stderr, " interface.\n"); return SYSERR; } host = &(nif->ip); /* Open HTTP device */ if (SYSERR == (long)open(httpdev, tcpdev)) { fprintf(stderr, "httpOpen SYSERR\n"); close(tcpdev); close(httpdev); return SYSERR; } /* Create web shell */ sprintf(thrname, "XWebShell_%d\0", (devtab[tcpdev].minor)); shelltid = create((void *)shell, INITSTK, INITPRIO, thrname, 3, httpdev, httpdev, DEVNULL); if (isbadtid(shelltid)) { fprintf(stderr, "shell creation bad tid\n"); close(httpdev); close(tcpdev); return SYSERR; } /* Spawn thread that will wait for kill httpserver thread signal */ sprintf(thrname, "XWebKillerD_%d\0", (devtab[tcpdev].minor)); killtid = create((void *)killHttpServer, INITSTK, INITPRIO, thrname, 3, httpdev, shelltid, tcpdev); /* Ready spawned threads */ ready(shelltid, RESCHED_NO); ready(killtid, RESCHED_NO); /* Open TCP device */ if (SYSERR == (long)open(tcpdev, host, NULL, HTTP_LOCAL_PORT, NULL, TCP_PASSIVE)) { int temp; sleep(TCP_TWOMSL + 500); if (SYSERR == (temp = (long)open(tcpdev, host, NULL, HTTP_LOCAL_PORT, NULL, TCP_PASSIVE))) { fprintf(CONSOLE, "open returns: %d\n", temp); fprintf(stderr, "tcpOpen SYSERR, devnum: %d\n", tcpdev); kill(shelltid); close(tcpdev); close(httpdev); return SYSERR; } fprintf(CONSOLE, "open returns: %d\n", temp); } /* Allocate TCP device */ gentcpdev = tcpAlloc(); if (isbadtcp(gentcpdev)) { /* Wait for a free http device */ wait(maxhttp); /* Acquire a TCP device through allocation */ while ((ushort)SYSERR == (gentcpdev = tcpAlloc())) { /* Do nothing, yield processor */ yield(); } /* Everything acquired, spawn general http device listener */ if (semcount(activeXWeb) <= 0) { sprintf(thrname, "XWeb_%d\0", (devtab[gentcpdev].minor)); ready(create((void *)httpServer, INITSTK, INITPRIO, thrname, 2, netDescrp, gentcpdev), RESCHED_NO); } /* Did not consume http device, just waited for availability */ signal(maxhttp); } else { sprintf(thrname, "XWeb_%d\0", (devtab[gentcpdev].minor)); ready(create((void *)httpServer, INITSTK, INITPRIO, thrname, 2, netDescrp, gentcpdev), RESCHED_NO); } return OK; }