diff -ruw old-lab4/GNUmakefile lab4/GNUmakefile --- old-lab4/GNUmakefile 2005-06-02 04:40:12.000000000 -0700 +++ lab4/GNUmakefile 2005-06-05 15:03:31.000000000 -0700 @@ -1,12 +1,16 @@ CC = gcc CFLAGS = -g -W -Wall +# Uncomment the following line to run on Solaris machines. +#LIBS = -lsocket -lnsl + all: miniserve cgi-bin/slow miniserve: httpparse.o miniserve.o - $(CC) $(CFLAGS) httpparse.o miniserve.o -o $@ + $(CC) $(CFLAGS) -o $@ httpparse.o miniserve.o $(LIBS) cgi-bin/slow: slow.c + @-test -d cgi-bin || mkdir cgi-bin $(CC) $(CFLAGS) $< -o $@ httpparse.o: httpparse.c miniserve.h @@ -14,6 +18,7 @@ clean: -rm -f miniserve cgi-bin/slow *.o *~ + @-test -d cgi-bin || mkdir cgi-bin distclean: clean diff -ruw old-lab4/httpparse.c lab4/httpparse.c --- old-lab4/httpparse.c 2005-06-02 04:33:52.000000000 -0700 +++ lab4/httpparse.c 2005-06-05 15:00:58.000000000 -0700 @@ -14,7 +14,7 @@ ****************************************************************************/ -static int http_helper_find_httpver(const char *buffer, int length); +static int http_helper_find_httpver(const char *buffer, int length, char **end); static int http_helper_find_httpend(const char *buffer, int length); static int http_helper_escape(struct Connection *conn, int length); @@ -92,13 +92,12 @@ } // look for HTTP/1.1 - r = http_helper_find_httpver(buf, len); + r = http_helper_find_httpver(buf, len, &end); if (r < 0) return r; - end = buf + r; - buf += (r + 9); - len -= (r + 9); + buf += r; + len -= r; // look for \r\n\r\n r = http_helper_find_httpend(buf, len); @@ -112,36 +111,27 @@ return http_helper_escape(conn, end - start); } -// Returns where in the buffer to find " HTTP/1.1\r\n" +// Returns where in the buffer to find " HTTP/1.[01]\r\n" // or -EINVAL / -EAGAIN static int -http_helper_find_httpver(const char *buffer, int length) +http_helper_find_httpver(const char *buffer, int length, char **endstore) { - int i; + const char *s, *end = buffer + length; - if (length < (int) strlen(" HTTP/1.1\r\n")) { - for (i = 0; i < length; i++) { - if (buffer[i] < ' ') - return -EINVAL; - if (buffer[i] == ' ') { - if (strncmp(buffer+i, " HTTP/1.1\r\n", 11-i) - == 0) + for (s = buffer; s < end; s++) + if (*s == ' ') { + int check = (end - s >= 11 ? 11 : end - s); + if (memcmp(s, " HTTP/1.1\r\n", check) == 0 + || memcmp(s, " HTTP/1.0\r\n", check) == 0) { + if (check == 11) { + *endstore = (char *) s; + return (s - buffer) + 9; + } else return -EAGAIN; + } else return -EINVAL; - } - } - - return -EAGAIN; - } - - for (i = 0; i <= length - 11; i++) { - if (strncmp(buffer + i, " HTTP/1.1\r\n", 11) == 0) - return i; - - // if we hit a funky character - if (buffer[i] <= ' ') + } else if (*s < ' ') return -EINVAL; - } return -EAGAIN; } diff -ruw old-lab4/miniserve.c lab4/miniserve.c --- old-lab4/miniserve.c 2005-06-02 04:40:12.000000000 -0700 +++ lab4/miniserve.c 2005-06-05 15:03:31.000000000 -0700 @@ -18,6 +18,7 @@ #include #include #include +#include #include "miniserve.h" // The port to listen on @@ -335,6 +336,7 @@ conn->wbuf_length = snprintf(conn->wbuf, conn->wbuf_capacity, "HTTP/1.1 200 OK\r\n\ Connection: close\r\n\ +Cache-Control: no-cache\r\n\ %s\ Content-Length: %u\r\n\ \r\n", mime_type, (unsigned) sb.st_size); @@ -538,7 +540,7 @@ if (argc == 3 && strcmp(argv[1], "-p") == 0 && (port = strtol(argv[2], &s, 0)) > 0 && port < 65536 && *s == 0) { - argc += 2; + argc -= 2; argv += 2; } if (argc > 1) diff -ruw old-lab4/slow.c lab4/slow.c --- old-lab4/slow.c 2005-06-02 04:28:21.000000000 -0700 +++ lab4/slow.c 2005-06-05 14:43:58.000000000 -0700 @@ -45,6 +45,7 @@ printf("HTTP/1.1 200 OK\r\n\ Connection: close\r\n\ +Cache-Control: no-cache\n\ %s\ Content-Length: %d\r\n\ \r\n", mime_type, (int) filesize);