Thread: [Assorted-commits] SF.net SVN: assorted: [472] nano-httpd/trunk/src/nanohttpd.cc
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-02-19 07:03:14
|
Revision: 472 http://assorted.svn.sourceforge.net/assorted/?rev=472&view=rev Author: yangzhang Date: 2008-02-18 23:03:19 -0800 (Mon, 18 Feb 2008) Log Message: ----------- ready to run in josmp Modified Paths: -------------- nano-httpd/trunk/src/nanohttpd.cc Modified: nano-httpd/trunk/src/nanohttpd.cc =================================================================== --- nano-httpd/trunk/src/nanohttpd.cc 2008-02-19 06:45:50 UTC (rev 471) +++ nano-httpd/trunk/src/nanohttpd.cc 2008-02-19 07:03:19 UTC (rev 472) @@ -52,33 +52,33 @@ do { \ char b_[4096]; \ int n_ = snprintf(b_, sizeof(b_), __VA_ARGS__); \ - if (send(s, b_, n_, 0) == 0) panic("XXX"); \ + if (lwip_send(s, b_, n_, 0) == -1) panic("XXX"); \ } while (0); #define SERVER "webserver/1.0" #define PROTOCOL "HTTP/1.0" #define RFC1123FMT "%a, %d %b %Y %H:%M:%S GMT" -#define PORT 80 +#define PORT 8000 // XXX void send_headers(int f, int status, char *title, char *extra, char *mime, - int length, time_t date) + int length, int date) { - time_t now; - char timebuf[128]; + //time_t now; + //char timebuf[128]; nprintf(f, "%s %d %s\r\n", PROTOCOL, status, title); nprintf(f, "Server: %s\r\n", SERVER); - now = time(NULL); - strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&now)); - nprintf(f, "Date: %s\r\n", timebuf); + //now = time(NULL); + //strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&now)); + //nprintf(f, "Date: %s\r\n", timebuf); if (extra) nprintf(f, "%s\r\n", extra); if (mime) nprintf(f, "Content-Type: %s\r\n", mime); if (length >= 0) nprintf(f, "Content-Length: %d\r\n", length); - if (date != -1) - { - strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&date)); - nprintf(f, "Last-Modified: %s\r\n", timebuf); - } + //if (date != -1) + //{ + // strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&date)); + // nprintf(f, "Last-Modified: %s\r\n", timebuf); + //} nprintf(f, "Connection: close\r\n"); nprintf(f, "\r\n"); } @@ -95,6 +95,9 @@ // XXX void send_file(int f, char *path) { + char msg[] = "hello, world!\n"; + send_headers(f, 200, "OK", NULL, "text/plain", sizeof(msg), -1); + if (lwip_send(f, msg, sizeof(msg), 0) == -1) panic("XXX"); // char data[4096]; // int n; // @@ -122,7 +125,7 @@ // char pathbuf[4096]; // int len; - if (recv(f, buf, sizeof(buf), MSG_WAITALL) < 0) return -1; + if (lwip_recv(f, buf, sizeof(buf), 0) < 0) return -1; printf("URL: %s", buf); method = strtok(buf, " "); @@ -200,10 +203,16 @@ int s = lwip_socket(PF_INET, SOCK_STREAM, 0); if (s < 0) panic("XXX"); +#if STANDALONE + int on = 1; + if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) + panic("XXX"); +#endif + struct sockaddr_in sa; bzero(&sa, sizeof(sa));; sa.sin_family = AF_INET; - sa.sin_port = htons(8000); + sa.sin_port = htons(PORT); sa.sin_addr.s_addr = htonl(INADDR_ANY); cprintf("bind\n"); @@ -218,6 +227,7 @@ cprintf("processing\n"); if (process(t) < 0) panic("XXX"); + lwip_close(t); } return 0; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <yan...@us...> - 2008-02-24 06:57:49
|
Revision: 494 http://assorted.svn.sourceforge.net/assorted/?rev=494&view=rev Author: yangzhang Date: 2008-02-23 22:57:52 -0800 (Sat, 23 Feb 2008) Log Message: ----------- cleanup Modified Paths: -------------- nano-httpd/trunk/src/nanohttpd.cc Modified: nano-httpd/trunk/src/nanohttpd.cc =================================================================== --- nano-httpd/trunk/src/nanohttpd.cc 2008-02-24 06:56:42 UTC (rev 493) +++ nano-httpd/trunk/src/nanohttpd.cc 2008-02-24 06:57:52 UTC (rev 494) @@ -1,17 +1,6 @@ -// Derived from: +// See also: // // http://www.jbox.dk/sanos/webserver.htm -// -// Example HTTP request -// -// GET / HTTP/1.1 -// User-Agent: Opera/9.25 (Windows NT 5.1; U; en) -// Host: harvard:8000 -// Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1 -// Accept-Language: en-US,en;q=0.9 -// Accept-Charset: iso-8859-1, utf-8, utf-16, *;q=0.1 -// Accept-Encoding: deflate, gzip, x-gzip, identity, *;q=0 -// Connection: Keep-Alive #define STANDALONE 1 @@ -63,22 +52,11 @@ void send_headers(int f, int status, char *title, char *extra, char *mime, int length, int date) { - //time_t now; - //char timebuf[128]; - nprintf(f, "%s %d %s\r\n", PROTOCOL, status, title); nprintf(f, "Server: %s\r\n", SERVER); - //now = time(NULL); - //strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&now)); - //nprintf(f, "Date: %s\r\n", timebuf); if (extra) nprintf(f, "%s\r\n", extra); if (mime) nprintf(f, "Content-Type: %s\r\n", mime); if (length >= 0) nprintf(f, "Content-Length: %d\r\n", length); - //if (date != -1) - //{ - // strftime(timebuf, sizeof(timebuf), RFC1123FMT, gmtime(&date)); - // nprintf(f, "Last-Modified: %s\r\n", timebuf); - //} nprintf(f, "Connection: close\r\n"); nprintf(f, "\r\n"); } @@ -92,26 +70,11 @@ nprintf(f, "</BODY></HTML>\r\n"); } -// XXX void send_file(int f, char *path) { char msg[] = "hello, world!\n"; send_headers(f, 200, "OK", NULL, "text/plain", sizeof(msg), -1); if (lwip_send(f, msg, sizeof(msg), 0) == -1) panic("XXX"); -// char data[4096]; -// int n; -// -// FILE *file = fopen(path, "r"); -// if (!file) -// send_error(f, 403, "Forbidden", NULL, "Access denied."); -// else -// { -// int length = S_ISREG(statbuf->st_mode) ? statbuf->st_size : -1; -// send_headers(f, 200, "OK", NULL, get_mime_type(path), length, statbuf->st_mtime); -// -// while ((n = fread(data, 1, sizeof(data), file)) > 0) fwrite(data, 1, n, f); -// fclose(file); -// } } int @@ -121,9 +84,6 @@ char *method; char *path; char *protocol; -// struct stat statbuf; -// char pathbuf[4096]; -// int len; if (lwip_recv(f, buf, sizeof(buf), 0) < 0) return -1; printf("URL: %s", buf); @@ -135,61 +95,6 @@ if (strcasecmp(method, "GET") != 0) send_error(f, 501, "Not supported", NULL, "Method is not supported."); -// else if (stat(path, &statbuf) < 0) -// send_error(f, 404, "Not Found", NULL, "File not found."); -// else if (S_ISDIR(statbuf.st_mode)) -// { -// len = strlen(path); -// if (len == 0 || path[len - 1] != '/') -// { -// snprintf(pathbuf, sizeof(pathbuf), "Location: %s/", path); -// send_error(f, 302, "Found", pathbuf, "Directories must end with a slash."); -// } -// else -// { -// snprintf(pathbuf, sizeof(pathbuf), "%sindex.html", path); -// if (stat(pathbuf, &statbuf) >= 0) -// send_file(f, pathbuf, &statbuf); -// else -// { -// DIR *dir; -// struct dirent *de; -// -// send_headers(f, 200, "OK", NULL, "text/html", -1, statbuf.st_mtime); -// nprintf(f, "<HTML><HEAD><TITLE>Index of %s</TITLE></HEAD>\r\n<BODY>", path); -// nprintf(f, "<H4>Index of %s</H4>\r\n<PRE>\n", path); -// nprintf(f, "Name Last Modified Size\r\n"); -// nprintf(f, "<HR>\r\n"); -// if (len > 1) nprintf(f, "<A HREF=\"..\">..</A>\r\n"); -// -// dir = opendir(path); -// while ((de = readdir(dir)) != NULL) -// { -// char timebuf[32]; -// struct tm *tm; -// -// strcpy(pathbuf, path); -// strcat(pathbuf, de->d_name); -// -// stat(pathbuf, &statbuf); -// tm = gmtime(&statbuf.st_mtime); -// strftime(timebuf, sizeof(timebuf), "%d-%b-%Y %H:%M:%S", tm); -// -// nprintf(f, "<A HREF=\"%s%s\">", de->d_name, S_ISDIR(statbuf.st_mode) ? "/" : ""); -// nprintf(f, "%s%s", de->d_name, S_ISDIR(statbuf.st_mode) ? "/</A>" : "</A> "); -// if (de->d_namlen < 32) nprintf(f, "%*s", 32 - de->d_namlen, ""); -// -// if (S_ISDIR(statbuf.st_mode)) -// nprintf(f, "%s\r\n", timebuf); -// else -// nprintf(f, "%s %10d\r\n", timebuf, statbuf.st_size); -// } -// closedir(dir); -// -// nprintf(f, "</PRE>\r\n<HR>\r\n<ADDRESS>%s</ADDRESS>\r\n</BODY></HTML>\r\n", SERVER); -// } -// } -// } else send_file(f, path); // , &statbuf); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |