HTTP Server, WebSocket support

Dependents:   YoPlegma

Fork of HTTPD by Suga koubou

Files at this revision

API Documentation at this revision

Comitter:
mitsarionas
Date:
Fri Sep 04 08:41:12 2015 +0000
Parent:
0:d18dff347122
Commit message:
redirect

Changed in this revision

HTTPD.h Show annotated file Show diff for this revision Revisions of this file
HTTPD_util.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/HTTPD.h	Wed Nov 13 01:58:04 2013 +0000
+++ b/HTTPD.h	Fri Sep 04 08:41:12 2015 +0000
@@ -32,7 +32,7 @@
 #define HTTPD_TIMEOUT 15000
 #define HTTPD_MAX_HANDLES 10
  
-#define HTTPD_CMD_SIZE 100
+#define HTTPD_CMD_SIZE 256
 #define HTTPD_BUF_SIZE 256
 #define HTTPD_STACK_SIZE (1024 * 6)
 //#define HTTPD_ENABLE_CLOSER
@@ -116,6 +116,7 @@
     int base64encode (const char *input, int length, char *output, int len);
     int urlencode (const char *str, char *buf, int len);
     int urldecode (const char *str, char *buf, int len);
+    int redirect (int id, int code, char *location, const char *body, int len, const char *header = NULL);
 
     static HTTPD * getInstance() {
         return _inst;
--- a/HTTPD_util.cpp	Wed Nov 13 01:58:04 2013 +0000
+++ b/HTTPD_util.cpp	Fri Sep 04 08:41:12 2015 +0000
@@ -80,6 +80,42 @@
     return _state[id].client->send_all((char*)body, len);
 }
 
+int HTTPD::redirect (int id, int code, char *location, const char *body, int len, const char *header) {
+    char buf[HTTPD_CMD_SIZE];
+    strcpy(buf, "HTTP/1.1 ");
+    switch (code) {
+        case 301:
+            strcat(buf, "301 Moved Permanently\r\n");
+            break;
+        case 302:
+            strcat(buf, "302 Found\r\n");
+            break;
+        case 307:
+            strcat(buf, "307 Temporary Redirect\r\n");
+            break;
+        default:
+            return -1;
+    }
+    _state[id].client->send_all(buf, strlen(buf));
+    strcpy(buf, "Server: GSwifi httpd\r\n");
+    _state[id].client->send_all(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));
+    sprintf(buf, "Location: %s\r\n\r\n", location);
+    _state[id].client->send_all(buf, strlen(buf));
+    if (header) {
+        _state[id].client->send_all((char*)header, strlen(header));
+    }
+    sprintf(buf, "Content-Length: %d\r\n\r\n", len);
+    _state[id].client->send_all(buf, strlen(buf));
+
+    return _state[id].client->send_all((char*)body, len);
+}
+
 int HTTPD::getHandler (const char *uri) {
     int i;