Browse code

webkernel: fix connection state machine

Dario Rodriguez authored on 24/06/2014 20:35:11
Showing 1 changed files
... ...
@@ -40,7 +40,6 @@ typedef struct wk_post {
40 40
 typedef enum wkc_status {
41 41
         wkc_header=0,
42 42
         wkc_post,
43
-        wkc_none,
44 43
 } wkc_status;
45 44
 
46 45
 typedef struct wk_client {
... ...
@@ -51,13 +50,13 @@ typedef struct wk_client {
51 50
         int sizeoutbufids;
52 51
         int usedoutbufids;
53 52
         int outbufids[MAXOUTBUF];
54
-        wkc_status status;
55
-        int serviced;
56
-        int continuationactive;
57
-        int fdtoserve;
53
+        int uriready;
58 54
         wk_uri uri;
59 55
         int headerbufid;
56
+        wkc_status status;
60 57
         int keepalive;
58
+        int continuationactive;
59
+        int fdtoserve;
61 60
         int sizepost;
62 61
         int usedpost;
63 62
         wk_post *post;
... ...
@@ -216,7 +215,7 @@ wk_geturi(wk *paramweb, int connid)
216 215
                 return(NULL);
217 216
         if((client=wk_clientget(web,connid))==NULL)
218 217
                 return(NULL);
219
-        if(client->serviced==0)
218
+        if(client->uriready==0)
220 219
                 return(NULL);
221 220
         return(&(client->uri));
222 221
 }
... ...
@@ -314,7 +313,7 @@ wk_service(wk *paramweb)
314 313
                                         if(client->continuationactive && web->callback_continuation!=NULL) {
315 314
                                                 client->continuationactive=web->callback_continuation((wk *)web,client->connid,&(client->uri),web->userptr);
316 315
                                         } else {
317
-                                                client->serviced=0; /* we have finished servicing this one */
316
+                                                client->uriready=0; /* we have finished servicing this one */
318 317
                                                 if(!client->keepalive)
319 318
                                                         wk_close((wk *)web, fd); /* all sent */
320 319
                                         }
... ...
@@ -886,16 +885,8 @@ wk_clientservicereadheader(_wk *web, wk_client *client)
886 885
         char *sep;
887 886
         wk_uri *uri;
888 887
         wk_action action;
889
-        /* check if we have all the headers */
890 888
         if((in=wk_sbufget((wk *)web, client->inbufid))==NULL)
891 889
                 return(-1); /* internal error */
892
-        if((end=str_findfirstempty(sbuf_ptr(in),sbuf_count(in)))==NULL) {
893
-                sbuf_discard(in);
894
-                if(sbuf_unused(in)==0)
895
-                        return(-1); /* header part too long */
896
-                /* incomplete headers, have to wait for more data */
897
-                return(0);
898
-        }
899 890
         /* get memory for the uri data */
900 891
         if(client->headerbufid!=-1)
901 892
                 wk_sbufrelease((wk *)web,client->headerbufid),client->headerbufid=-1;
... ...
@@ -903,7 +894,17 @@ wk_clientservicereadheader(_wk *web, wk_client *client)
903 894
                 return(-1); /* insufficient memory */
904 895
         if((hbuf=wk_sbufget((wk *)web,client->headerbufid))==NULL)
905 896
                 return(-1); /* internal error */
897
+        /* check if we have all the headers */
898
+        if((end=str_findfirstempty(sbuf_ptr(in),sbuf_count(in)))==NULL) {
899
+                sbuf_discard(in);
900
+                if(sbuf_unused(in)==0)
901
+                        return(-1); /* header part too long */
902
+                /* incomplete headers, have to wait for more data */
903
+                return(0);
904
+        }
906 905
         /* prepare to fill the uri struct */
906
+        client->uriready=0;
907
+        sbuf_wipe(hbuf);
907 908
         uri=&(client->uri);
908 909
         memset(uri,0,sizeof(wk_uri));
909 910
         /* check that the method is supported */
... ...
@@ -955,13 +956,13 @@ wk_clientservicereadheader(_wk *web, wk_client *client)
955 956
         /* add header terminator */
956 957
         sbuf_add(hbuf,"",1);
957 958
         /* call the http method */
958
-        client->serviced=1;
959
+        client->uriready=1;
959 960
         client->continuationactive=0;
960 961
         action=web->callback_http((wk *)web, client->connid, uri, web->userptr);
961 962
         if(action==wkact_continuation) {
962
-                client->serviced=1;
963
+                client->uriready=1;
963 964
                 client->continuationactive=1;
964
-                client->status=wkc_none;
965
+                client->status=wkc_header;
965 966
         } else if(action==wkact_post && web->callback_post!=NULL) {
966 967
                 char *lenvar;
967 968
                 char *contenttype;
... ...
@@ -975,15 +976,15 @@ wk_clientservicereadheader(_wk *web, wk_client *client)
975 976
                 }
976 977
                 while(*lenvar==' ' || *lenvar=='\t')
977 978
                         lenvar++;
978
-                client->serviced=1;
979
+                client->uriready=1;
979 980
                 client->continuationactive=0;
980 981
                 client->status=wkc_post;
981 982
                 client->pendingpost=atoi(lenvar);
982 983
                 client->inpostvar=-1;
983 984
         } else {
984
-                client->serviced=1;
985
+                client->uriready=1;
985 986
                 client->continuationactive=0;
986
-                client->status=wkc_none;
987
+                client->status=wkc_header;
987 988
         }
988 989
         return(0);
989 990
 }
... ...
@@ -1038,17 +1039,17 @@ wk_clientservicereadpost(_wk *web, wk_client *client)
1038 1039
         if(client->pendingpost>0)
1039 1040
                 return(0); /* nothing more to do for now*/
1040 1041
         /* call the post method */
1041
-        client->serviced=1;
1042
+        client->uriready=1;
1042 1043
         client->continuationactive=0;
1043 1044
         action=web->callback_post((wk *)web, client->connid, client->uri, web->userptr);
1044 1045
         if(action==wkact_continuation) {
1045
-                client->serviced=1;
1046
+                client->uriready=1;
1046 1047
                 client->continuationactive=1;
1047
-                client->status=wkc_none;
1048
+                client->status=wkc_header;
1048 1049
         } else {
1049
-                client->serviced=1;
1050
+                client->uriready=1;
1050 1051
                 client->continuationactive=0;
1051
-                client->status=wkc_none;
1052
+                client->status=wkc_header;
1052 1053
         }
1053 1054
         return(0);
1054 1055
 }