Fixed another htparser bug.
authorFredrik Tolf <fredrik@dolda2000.com>
Wed, 17 Dec 2008 03:27:28 +0000 (04:27 +0100)
committerFredrik Tolf <fredrik@dolda2000.com>
Wed, 17 Dec 2008 03:34:16 +0000 (04:34 +0100)
src/debugsink.c
src/htparser.c

index 44db679..eb389f3 100644 (file)
 int main(int argc, char **argv)
 {
     struct hthead *req;
-    int fd;
+    int fd, ret;
+    char buf[1024];
     FILE *out;
+    off_t nb;
     
     while(1) {
        if((fd = recvreq(0, &req)) < 0) {
@@ -44,12 +46,35 @@ int main(int argc, char **argv)
        }
        printf("%s %s %s %s %s\n", req->method, req->url, req->ver, req->rest, getheader(req, "x-ash-address"));
        out = fdopen(fd, "w");
-       fprintf(out, "HTTP/1.1 200 OK\r\n");
-       fprintf(out, "Content-Type: text/plain; charset=utf8\r\n");
-       fprintf(out, "\r\n");
-       fprintf(out, "%s\n", getheader(req, "x-ash-port"));
-       fprintf(out, "%s\n", getheader(req, "user-agent"));
-       fprintf(out, "%s\n", getheader(req, "content-length"));
+       if(!strcmp(req->rest, "")) {
+           fprintf(out, "HTTP/1.1 200 OK\r\n");
+           fprintf(out, "Content-Type: text/html; charset=utf8\r\n");
+           fprintf(out, "\r\n");
+           fprintf(out, "<html>\n<body>\n<form action=\"/post\" method=\"post\">\n<input type=\"submit\" name=\"barda\" />\n</form>\n</body>\n</html>\n");
+       } else if(!strcmp(req->rest, "post")) {
+           nb = 0;
+           while(1) {
+               ret = read(fd, buf, 1024);
+               if(ret < 0)
+                   exit(1);
+               if(ret == 0)
+                   break;
+               nb += ret;
+           }
+           fprintf(out, "HTTP/1.1 200 OK\r\n");
+           fprintf(out, "Content-Type: text/plain; charset=utf8\r\n");
+           fprintf(out, "\r\n");
+           fprintf(out, "%i\n", (int)nb);
+       } else if(!strcmp(req->rest, "inf")) {
+           fprintf(out, "HTTP/1.1 200 OK\r\n");
+           fprintf(out, "Content-Type: text/plain\r\n");
+           fprintf(out, "\r\n");
+           while(1)
+               fprintf(out, "0123456789012345678901234567890123456789012345678901234567890123456789\n");
+       } else {
+           fprintf(out, "HTTP/1.1 404 Not Found\r\n");
+           fprintf(out, "\r\n");
+       }
        fclose(out);
     }
     return(0);
index 88c8e6d..a96b7b4 100644 (file)
@@ -349,7 +349,7 @@ static void serve(struct muth *muth, va_list args)
     bufinit(inbuf);
     bufinit(outbuf);
     cfd = -1;
-    req = NULL;
+    req = resp = NULL;
     while(1) {
        /*
         * First, find and decode the header:
@@ -388,7 +388,8 @@ static void serve(struct muth *muth, va_list args)
            headappheader(req, "X-Ash-Address", inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&name)->sin6_addr, nmbuf, sizeof(nmbuf)));
            headappheader(req, "X-Ash-Port", sprintf3("%i", ntohs(((struct sockaddr_in6 *)&name)->sin6_port)));
        }
-       cfd = sendreq(plex, req);
+       if((cfd = sendreq(plex, req)) < 0)
+           goto out;
 
        /*
         * If there is message data, pass it:
@@ -405,7 +406,8 @@ static void serve(struct muth *muth, va_list args)
         * Find and decode the response header:
         */
        outbuf.d = 0;
-       headoff = readhead(cfd, &outbuf);
+       if((headoff = readhead(cfd, &outbuf)) < 0)
+           goto out;
        hd = memcpy(smalloc(headoff + 1), outbuf.b, headoff);
        hd[headoff] = 0;
        if((resp = parserawresp(hd)) == NULL)