Khoa Bui Anh / HttpServer_snapshot_mbed-os

Fork of HttpServer_snapshot_mbed-os by Renesas

Files at this revision

API Documentation at this revision

Comitter:
dkato
Date:
Fri Aug 21 02:07:48 2015 +0000
Parent:
5:b8f6a11c70db
Child:
7:62bfc317772b
Commit message:
Add snapshot handler.

Changed in this revision

HTTPRequestHandler.cpp Show annotated file Show diff for this revision Revisions of this file
HTTPRequestHandler.h Show annotated file Show diff for this revision Revisions of this file
HTTPServer.h Show annotated file Show diff for this revision Revisions of this file
Handler/SnapshotHandler.cpp Show annotated file Show diff for this revision Revisions of this file
Handler/SnapshotHandler.h Show annotated file Show diff for this revision Revisions of this file
--- a/HTTPRequestHandler.cpp	Sat Feb 22 05:51:59 2014 +0000
+++ b/HTTPRequestHandler.cpp	Fri Aug 21 02:07:48 2015 +0000
@@ -145,9 +145,6 @@
 
 void HTTPRequestHandler::readHeaders()
 {
-    static char line[128];
-    static char key[128];
-    static char value[128];
     while( readLine(line, 128) > 0) { //if == 0, it is an empty line = end of headers
         int n = sscanf(line, "%[^:]: %[^\n]", key, value);
         if ( n == 2 ) {
@@ -162,8 +159,6 @@
 
 void HTTPRequestHandler::writeHeaders() //Called at the first writeData call
 {
-    static char line[128];
-
     //Response line
     sprintf(line, "HTTP/1.1 %d MbedInfo\r\n", m_errc); //Not a violation of the standard not to include the descriptive text
     m_pTCPSocketConnection->send(line, strlen(line));
@@ -187,7 +182,7 @@
     int len = 0;
     for(int i = 0; i < maxLen - 1; i++) {
         ret = m_pTCPSocketConnection->receive(str, 1);
-        if(!ret) {
+        if(ret <= 0) {
             break;
         }
         if( (len > 1) && *(str-1)=='\r' && *str=='\n' ) {
--- a/HTTPRequestHandler.h	Sat Feb 22 05:51:59 2014 +0000
+++ b/HTTPRequestHandler.h	Fri Aug 21 02:07:48 2015 +0000
@@ -97,6 +97,10 @@
   
   int readLine(char* str, int maxLen);
 
+  char line[128];
+  char key[128];
+  char value[128];
+
 };
 
 #endif
--- a/HTTPServer.h	Sat Feb 22 05:51:59 2014 +0000
+++ b/HTTPServer.h	Fri Aug 21 02:07:48 2015 +0000
@@ -272,7 +272,8 @@
 
         t = -1;
         for (i = 0; i < THREAD_MAX; i ++) {
-            if (threads[i] == NULL || threads[i]->get_state() == Thread::WaitingAnd) {
+            if ((threads[i] == NULL)
+             || ((threads[i]->get_state() == Thread::WaitingAnd) && (*clients[i].get_address() == 0))) {
                 if (t < 0) t = i; // next empty thread
             }
         }
@@ -282,5 +283,6 @@
 #include "Handler/RPCHandler.h"
 #include "Handler/FSHandler.h"
 #include "Handler/SimpleHandler.h"
+#include "SnapshotHandler.h"
 
 #endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Handler/SnapshotHandler.cpp	Fri Aug 21 02:07:48 2015 +0000
@@ -0,0 +1,129 @@
+/*******************************************************************************
+* DISCLAIMER
+* This software is supplied by Renesas Electronics Corporation and is only
+* intended for use with Renesas products. No other uses are authorized. This
+* software is owned by Renesas Electronics Corporation and is protected under
+* all applicable laws, including copyright laws.
+* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
+* THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
+* LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
+* AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
+* TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
+* ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
+* FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
+* ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
+* BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
+* Renesas reserves the right, without notice, to make changes to this software
+* and to discontinue the availability of this software. By using this software,
+* you agree to the additional terms and conditions found by accessing the
+* following link:
+* http://www.renesas.com/disclaimer*
+* Copyright (C) 2015 Renesas Electronics Corporation. All rights reserved.
+*******************************************************************************/
+
+#include "SnapshotHandler.h"
+
+#define CHUNK_SIZE           (128)
+
+int (*SnapshotHandler::callback_func_req)(const char ** pp_data);
+Semaphore SnapshotHandler::req_sem(1);
+
+
+SnapshotHandler::SnapshotHandler(const char* rootPath, const char* path, TCPSocketConnection* pTCPSocketConnection) : HTTPRequestHandler(rootPath, path, pTCPSocketConnection), m_err404(false)
+{
+}
+
+SnapshotHandler::~SnapshotHandler()
+{
+}
+
+void SnapshotHandler::doGet()
+{
+    const char * p_data = NULL;
+    int size = 0;
+
+    req_sem.wait();
+    if (callback_func_req != NULL) {
+        size = callback_func_req(&p_data);
+    }
+
+    if ((p_data == NULL) || (size <= 0)) {
+        m_err404 = true;
+        setErrCode(404);
+        const char* msg = "File not found.";
+        setContentLen(strlen(msg));
+        respHeaders()["Content-Type"] = "text/html";
+        respHeaders()["Connection"] = "close";
+        writeData(msg,strlen(msg)); //Only send header
+        printf("\r\nExit SnapshotHandler::doGet() w Error 404\r\n");
+        req_sem.release();
+        return;
+    }
+    send_data_buf = p_data;
+    send_size = size;
+    send_index = 0;
+
+    //Response
+    setContentLen(send_size);
+
+    //Make sure that the browser won't cache this request
+    respHeaders()["Cache-Control"] = "no-store";
+    respHeaders()["Pragma"] = "no-cache";
+    respHeaders()["Expires"] = "0";
+
+    //Write data
+    respHeaders()["Connection"] = "close";
+    onWriteable();
+
+    req_sem.release();
+}
+
+void SnapshotHandler::doPost()
+{
+
+}
+
+void SnapshotHandler::doHead()
+{
+
+}
+
+void SnapshotHandler::onReadable() //Data has been read
+{
+
+}
+
+void SnapshotHandler::onWriteable() //Data has been written & buf is free
+{
+    if (m_err404) {
+        //Error has been served, now exit
+        close();
+    } else {
+        while (true) {
+            int len = send_size - send_index;
+            if (len > CHUNK_SIZE) {
+                len = CHUNK_SIZE;
+            }
+            if (len > 0) {
+                int writtenLen = writeData((char *)&send_data_buf[send_index], len);
+                if (writtenLen < 0) { //Socket error
+                    close();
+                    return;
+                } else if (writtenLen < len) { //Short write, socket's buffer is full
+                    send_index += writtenLen;
+                    return;
+                } else {
+                    send_index += writtenLen;
+                }
+            } else {
+                close();
+                return;
+            }
+        }
+    }
+}
+
+void SnapshotHandler::onClose() //Connection is closing
+{
+    //Nothing to do
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Handler/SnapshotHandler.h	Fri Aug 21 02:07:48 2015 +0000
@@ -0,0 +1,59 @@
+/*******************************************************************************
+* DISCLAIMER
+* This software is supplied by Renesas Electronics Corporation and is only
+* intended for use with Renesas products. No other uses are authorized. This
+* software is owned by Renesas Electronics Corporation and is protected under
+* all applicable laws, including copyright laws.
+* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
+* THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
+* LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
+* AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
+* TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
+* ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
+* FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
+* ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
+* BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
+* Renesas reserves the right, without notice, to make changes to this software
+* and to discontinue the availability of this software. By using this software,
+* you agree to the additional terms and conditions found by accessing the
+* following link:
+* http://www.renesas.com/disclaimer*
+* Copyright (C) 2015 Renesas Electronics Corporation. All rights reserved.
+*******************************************************************************/
+
+#ifndef SNAPSHOT_HANDLER_H
+#define SNAPSHOT_HANDLER_H
+
+#include "HTTPRequestHandler.h"
+
+class SnapshotHandler : public HTTPRequestHandler
+{
+public:
+    SnapshotHandler(const char* rootPath, const char* path, TCPSocketConnection* pTCPSocketConnection);
+    virtual ~SnapshotHandler();
+
+    static void attach_req(int(*fptr)(const char ** pp_data)) {
+        callback_func_req = fptr;
+    }
+
+//protected:
+    static inline HTTPRequestHandler* inst(const char* rootPath, const char* path, TCPSocketConnection* pTCPSocketConnection) { return new SnapshotHandler(rootPath, path, pTCPSocketConnection); } //if we ever could do static virtual functions, this would be one
+
+    virtual void doGet();
+    virtual void doPost();
+    virtual void doHead();
+
+    virtual void onReadable(); //Data has been read
+    virtual void onWriteable(); //Data has been written & buf is free
+    virtual void onClose(); //Connection is closing
+
+private:
+    static int (*callback_func_req)(const char ** pp_data);
+    static Semaphore req_sem;
+    const char * send_data_buf;
+    int send_size;
+    int send_index;
+    bool m_err404;
+};
+
+#endif