HTTP Server, WebSocket support

Fork of HTTPD by Suga koubou

Revision:
1:b724fdb741e7
Parent:
0:d18dff347122
--- a/HTTPD_req.cpp	Wed Nov 13 01:58:04 2013 +0000
+++ b/HTTPD_req.cpp	Thu Jun 15 20:17:24 2017 +0000
@@ -37,7 +37,7 @@
     fp = fopen(file, "r");
     if (fp) {
         strcpy(buf, "HTTP/1.1 200 OK\r\n");
-        _state[id].client->send_all(buf, strlen(buf));
+        _state[id].client->send(buf, strlen(buf));
         {
             // file size
             i = ftell(fp);
@@ -47,22 +47,22 @@
         }
 
         strcpy(buf, "Server: GSwifi httpd\r\n");
-        _state[id].client->send_all(buf, strlen(buf));
+        _state[id].client->send(buf, strlen(buf));
         if (_state[id].keepalive) {
             strcpy(buf, "Connection: Keep-Alive\r\n");
         } else {
             strcpy(buf, "Connection: close\r\n");
         }
-        _state[id].client->send_all(buf, strlen(buf));
+        _state[id].client->send(buf, strlen(buf));
         sprintf(buf, "Content-Type: %s\r\n", mimetype(file));
-        _state[id].client->send_all(buf, strlen(buf));
+        _state[id].client->send(buf, strlen(buf));
         sprintf(buf, "Content-Length: %d\r\n\r\n", len);
-        _state[id].client->send_all(buf, strlen(buf));
+        _state[id].client->send(buf, strlen(buf));
 
         for (;;) {
             i = fread(buf, sizeof(char), sizeof(buf), fp);
             if (i <= 0) break;
-            _state[id].client->send_all(buf, i);
+            _state[id].client->send(buf, i);
 #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368)
             if (feof(fp)) break;
 #endif
@@ -70,7 +70,7 @@
         fclose(fp);
         return 0;
     }
-
+    
     httpdError(id, 404);
     return -1;
 }
@@ -96,21 +96,20 @@
     DBG("httpd error: %d %d %s\r\n", id, err, msg);
     
     sprintf(buf, "HTTP/1.1 %d %s\r\n", err, msg);
-    _state[id].client->send_all(buf, strlen(buf));
+    _state[id].client->send(buf, strlen(buf));
     strcpy(buf, "Content-Type: text/html\r\n\r\n");
-    _state[id].client->send_all(buf, strlen(buf));
+    _state[id].client->send(buf, strlen(buf));
 
     sprintf(buf, "<html><head><title>%d %s</title></head>\r\n", err, msg);
-    _state[id].client->send_all(buf, strlen(buf));
+    _state[id].client->send(buf, strlen(buf));
     sprintf(buf, "<body><h1>%s</h1></body></html>\r\n", msg);
-    _state[id].client->send_all(buf, strlen(buf));
+    _state[id].client->send(buf, strlen(buf));
     wait_ms(100);
     _state[id].client->close();
-//    WARN("%d.%d.%d.%d ", _httpd[cid].host.getIp()[0], _httpd[cid].host.getIp()[1], _httpd[cid].host.getIp()[2], _httpd[cid].host.getIp()[3]);
-//    WARN("%s %s %d %d -\r\n", _httpd[cid].type == GSPROT_HTTPGET ? "GET" : "POST", _httpd[cid].uri, _httpd[cid].length, err);
+    //WARN("%d.%d.%d.%d ", _httpd[cid].host.getIp()[0], _httpd[cid].host.getIp()[1], _httpd[cid].host.getIp()[2], _httpd[cid].host.getIp()[3]);
+    //WARN("%s %s %d %d -\r\n", _httpd[cid].type == GSPROT_HTTPGET ? "GET" : "POST", _httpd[cid].uri, _httpd[cid].length, err);
 }
 
-
 void HTTPD::recvData (int id, char c) {
 
     switch (_state[id].mode) {
@@ -192,15 +191,15 @@
 
     if (_state[id].mode == MODE_ENTER) {
         int i = getHandler(_state[id].uri);
+        printf("handler = %d, uri = %s\r\n", i, _state[id].uri);
         if (i >= 0) { 
             if (_handler[i].dir) {
                 // file
                 httpdFile(id, _handler[i].dir);
-            } else
-            if (_handler[i].funcCgi) {
+            } else if (_handler[i].funcCgi) {
                 // cgi
                 _handler[i].funcCgi(id);
-//                _state[id].keepalive = 0;
+                _state[id].keepalive = 0;
             } else {
                 httpdError(id, 403);
             }
@@ -210,7 +209,7 @@
 
         if (_state[id].keepalive) {
             DBG("keepalive %d", _state[id].keepalive);
-            _state[id].keepalive --;
+            _state[id].keepalive--;
         } else {
             _state[id].client->close();
         }