lib: Moved parseresponse from htparser into lib for reusage.
[ashd.git] / src / htparser.c
index 5179a25..a195fab 100644 (file)
@@ -77,6 +77,8 @@ static struct hthead *parsereq(FILE *in)
            goto fail;
        } else {
            bufadd(method, c);
+           if(method.d >= 128)
+               goto fail;
        }
     }
     while(1) {
@@ -87,6 +89,8 @@ static struct hthead *parsereq(FILE *in)
            goto fail;
        } else {
            bufadd(url, c);
+           if(url.d >= 65536)
+               goto fail;
        }
     }
     while(1) {
@@ -98,6 +102,8 @@ static struct hthead *parsereq(FILE *in)
            goto fail;
        } else {
            bufadd(ver, c);
+           if(ver.d >= 128)
+               goto fail;
        }
     }
     bufadd(method, 0);
@@ -121,66 +127,6 @@ out:
     return(req);
 }
 
-static struct hthead *parseresp(FILE *in)
-{
-    struct hthead *req;
-    int code;
-    struct charbuf ver, msg;
-    int c;
-    
-    req = NULL;
-    bufinit(ver);
-    bufinit(msg);
-    code = 0;
-    while(1) {
-       c = getc(in);
-       if(c == ' ') {
-           break;
-       } else if((c == EOF) || (c < 32) || (c >= 128)) {
-           goto fail;
-       } else {
-           bufadd(ver, c);
-       }
-    }
-    while(1) {
-       c = getc(in);
-       if(c == ' ') {
-           break;
-       } else if((c == EOF) || (c < '0') || (c > '9')) {
-           goto fail;
-       } else {
-           code = (code * 10) + (c - '0');
-       }
-    }
-    while(1) {
-       c = getc(in);
-       if(c == 10) {
-           break;
-       } else if(c == 13) {
-       } else if((c == EOF) || (c < 32)) {
-           goto fail;
-       } else {
-           bufadd(msg, c);
-       }
-    }
-    bufadd(msg, 0);
-    bufadd(ver, 0);
-    req = mkresp(code, msg.b, ver.b);
-    if(parseheaders(req, in))
-       goto fail;
-    goto out;
-    
-fail:
-    if(req != NULL) {
-       freehthead(req);
-       req = NULL;
-    }
-out:
-    buffree(msg);
-    buffree(ver);
-    return(req);
-}
-
 static off_t passdata(FILE *in, FILE *out, off_t max)
 {
     size_t read;
@@ -228,12 +174,50 @@ static int hasheader(struct hthead *head, char *name, char *val)
     return(!strcasecmp(hd, val));
 }
 
+static int canonreq(struct hthead *req)
+{
+    char *p, *p2, *r;
+    int n;
+
+    if(req->url[0] == '/') {
+       replrest(req, req->url + 1);
+       if((p = strchr(req->rest, '?')) != NULL)
+           *p = 0;
+       return(1);
+    }
+    if((p = strstr(req->url, "://")) != NULL) {
+       n = p - req->url;
+       if(((n == 4) && !strncasecmp(req->url, "http", 4)) ||
+          ((n == 5) && !strncasecmp(req->url, "https", 5))) {
+           if(getheader(req, "host"))
+               return(0);
+           p += 3;
+           if((p2 = strchr(p, '/')) == NULL) {
+               headappheader(req, "Host", p);
+               free(req->url);
+               req->url = sstrdup("/");
+           } else {
+               r = sstrdup(p2);
+               *(p2++) = 0;
+               headappheader(req, "Host", p);
+               free(req->url);
+               req->url = r;
+           }
+           replrest(req, req->url + 1);
+           if((p = strchr(req->rest, '?')) != NULL)
+               *p = 0;
+           return(1);
+       }
+    }
+    return(0);
+}
+
 void serve(FILE *in, struct conn *conn)
 {
     int pfds[2];
     FILE *out;
     struct hthead *req, *resp;
-    char *hd, *p;
+    char *hd;
     off_t dlen;
     
     out = NULL;
@@ -241,11 +225,8 @@ void serve(FILE *in, struct conn *conn)
     while(1) {
        if((req = parsereq(in)) == NULL)
            break;
-       replrest(req, req->url);
-       if(req->rest[0] == '/')
-           replrest(req, req->rest + 1);
-       if((p = strchr(req->rest, '?')) != NULL)
-           *p = 0;
+       if(!canonreq(req))
+           break;
        
        if((conn->initreq != NULL) && conn->initreq(conn, req))
            break;
@@ -271,7 +252,7 @@ void serve(FILE *in, struct conn *conn)
        /* Make sure to send EOF */
        shutdown(pfds[1], SHUT_WR);
        
-       if((resp = parseresp(out)) == NULL)
+       if((resp = parseresponse(out)) == NULL)
            break;
        replstr(&resp->ver, req->ver);