* use modern functions for downloads.
[citadel.git] / webcit / webcit.c
index fdde5ef53de869668d7f243aefc24c420198f9d9..a47f5f31eca2064a602f049229a1d861bf6ff95b 100644 (file)
@@ -283,7 +283,7 @@ void print_menu_box(char* Title, char *Class, int nLines, ...)
 /*
  * dump out static pages from disk
  */
-void output_static(char *what)
+void output_static(const char *what)
 {
        int fd;
        struct stat statbuf;
@@ -540,7 +540,11 @@ int is_mobile_ua(char *user_agent) {
 /*
  * Entry point for WebCit transaction
  */
-void session_loop(HashList *HTTPHeaders, StrBuf *ReqLine, StrBuf *request_method, StrBuf *ReadBuf)
+void session_loop(HashList *HTTPHeaders, 
+                 StrBuf *ReqLine, 
+                 StrBuf *request_method, 
+                 StrBuf *ReadBuf,
+                 const char **Pos)
 {
        StrBuf *Buf;
        const char *pch, *pchs, *pche;
@@ -564,6 +568,8 @@ void session_loop(HashList *HTTPHeaders, StrBuf *ReqLine, StrBuf *request_method
        int len = 0;
        void *vHandler;
        WebcitHandler *Handler;
+       struct timeval tx_start;
+       struct timeval tx_finish;
 
        /*
         * We stuff these with the values coming from the client cookies,
@@ -576,6 +582,8 @@ void session_loop(HashList *HTTPHeaders, StrBuf *ReqLine, StrBuf *request_method
        StrBuf *c_httpauth_user;
        StrBuf *c_httpauth_pass;
        wcsession *WCC;
+
+       gettimeofday(&tx_start, NULL);          /* start a stopwatch for performance timing */
        
        Buf = NewStrBuf();
        c_username = NewStrBuf();
@@ -690,7 +698,11 @@ void session_loop(HashList *HTTPHeaders, StrBuf *ReqLine, StrBuf *request_method
                body_start = StrLength(content);
 
                /** Read the entire input data at once. */
-               client_read(&WCC->http_sock, content, ReadBuf, ContentLength + body_start);
+               client_read_to(&WCC->http_sock, 
+                              content, 
+                              ReadBuf, Pos,
+                              ContentLength,
+                              SLEEPING);
 
                if (!strncasecmp(ChrPtr(ContentType), "application/x-www-form-urlencoded", 33)) {
                        StrBufCutLeft(content, body_start);
@@ -833,6 +845,16 @@ void session_loop(HashList *HTTPHeaders, StrBuf *ReqLine, StrBuf *request_method
                        }
                        if (WCC->serv_info == NULL)
                                WCC->serv_info = get_serv_info(browser_host, user_agent);
+                       if (WCC->serv_info == NULL){
+                               begin_burst();
+                               wprintf(_("Received unexpected answer from Citadel "
+                                         "server; bailing out."));
+                               hprintf("HTTP/1.1 200 OK\r\n");
+                               hprintf("Content-type: text/plain; charset=utf-8\r\n");
+                               end_burst();
+                               end_webcit_session();
+                               goto SKIP_ALL_THIS_CRAP;
+                       }
                        if (WCC->serv_info->serv_rev_level < MINIMUM_CIT_VERSION) {
                                begin_burst();
                                wprintf(_("You are connected to a Citadel "
@@ -845,6 +867,8 @@ void session_loop(HashList *HTTPHeaders, StrBuf *ReqLine, StrBuf *request_method
                                                MINIMUM_CIT_VERSION / 100,
                                                MINIMUM_CIT_VERSION % 100
                                        );
+                               hprintf("HTTP/1.1 200 OK\r\n");
+                               hprintf("Content-type: text/plain; charset=utf-8\r\n");
                                end_burst();
                                end_webcit_session();
                                goto SKIP_ALL_THIS_CRAP;
@@ -1045,6 +1069,14 @@ SKIP_ALL_THIS_CRAP:
        }
        FreeStrBuf(&WCC->trailing_javascript);
        WCC->http_host = NULL;
+
+       /* How long did this transaction take? */
+       gettimeofday(&tx_finish, NULL);
+       
+       lprintf(9, "Transaction completed in %ld.%06ld seconds.\n",
+               ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) / 1000000,
+               ((tx_finish.tv_sec*1000000 + tx_finish.tv_usec) - (tx_start.tv_sec*1000000 + tx_start.tv_usec)) % 1000000
+       );
 }