Fork of HttpServer_snapshot_mbed-os library
Fork of HttpServer_snapshot_mbed-os by
SnapshotHandler.cpp
00001 /******************************************************************************* 00002 * DISCLAIMER 00003 * This software is supplied by Renesas Electronics Corporation and is only 00004 * intended for use with Renesas products. No other uses are authorized. This 00005 * software is owned by Renesas Electronics Corporation and is protected under 00006 * all applicable laws, including copyright laws. 00007 * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING 00008 * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT 00009 * LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 00010 * AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED. 00011 * TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS 00012 * ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE 00013 * FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR 00014 * ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE 00015 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 00016 * Renesas reserves the right, without notice, to make changes to this software 00017 * and to discontinue the availability of this software. By using this software, 00018 * you agree to the additional terms and conditions found by accessing the 00019 * following link: 00020 * http://www.renesas.com/disclaimer* 00021 * Copyright (C) 2015 Renesas Electronics Corporation. All rights reserved. 00022 *******************************************************************************/ 00023 00024 #include "SnapshotHandler.h" 00025 00026 int (*SnapshotHandler::callback_func_req)(const char ** pp_data); 00027 int (*SnapshotHandler::callback_func_req2)(const char* rootPath, const char* path, const char ** pp_data); 00028 void (*SnapshotHandler::callback_func_send_end)(const char* rootPath, const char* path, const char * p_data); 00029 Semaphore SnapshotHandler::req_sem(1); 00030 00031 00032 SnapshotHandler::SnapshotHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket), m_err404(false) 00033 { 00034 } 00035 00036 SnapshotHandler::~SnapshotHandler() 00037 { 00038 } 00039 00040 void SnapshotHandler::doGet() 00041 { 00042 const char * p_data = NULL; 00043 int size = 0; 00044 00045 req_sem.wait(); 00046 if (callback_func_req != NULL) { 00047 size = callback_func_req(&p_data); 00048 } 00049 if (callback_func_req2 != NULL) { 00050 size = callback_func_req2((rootPath()).c_str(), (path()).c_str(), &p_data); 00051 } 00052 00053 if ((p_data == NULL) || (size <= 0)) { 00054 if (callback_func_send_end != NULL) { 00055 callback_func_send_end((rootPath()).c_str(), (path()).c_str(), p_data); 00056 } 00057 m_err404 = true; 00058 setErrCode(404); 00059 const char* msg = "File not found."; 00060 setContentLen(strlen(msg)); 00061 respHeaders()["Content-Type"] = "text/html"; 00062 respHeaders()["Connection"] = "close"; 00063 writeData(msg,strlen(msg)); //Only send header 00064 printf("\r\nExit SnapshotHandler::doGet() w Error 404\r\n"); 00065 req_sem.release(); 00066 return; 00067 } 00068 send_data_buf = p_data; 00069 send_size = size; 00070 send_index = 0; 00071 00072 //Response 00073 setContentLen(send_size); 00074 00075 //Make sure that the browser won't cache this request 00076 respHeaders()["Cache-Control"] = "no-store"; 00077 respHeaders()["Pragma"] = "no-cache"; 00078 respHeaders()["Expires"] = "0"; 00079 00080 //Write data 00081 respHeaders()["Connection"] = "close"; 00082 onWriteable(); 00083 00084 req_sem.release(); 00085 } 00086 00087 void SnapshotHandler::doPost() 00088 { 00089 00090 } 00091 00092 void SnapshotHandler::doHead() 00093 { 00094 00095 } 00096 00097 void SnapshotHandler::onReadable() //Data has been read 00098 { 00099 00100 } 00101 00102 void SnapshotHandler::onWriteable() //Data has been written & buf is free 00103 { 00104 if (m_err404) { 00105 //Error has been served, now exit 00106 close(); 00107 } else { 00108 while (true) { 00109 int len = send_size - send_index; 00110 if (len > 0) { 00111 int writtenLen = writeData((char *)&send_data_buf[send_index], len); 00112 if (writtenLen < 0) { //Socket error 00113 close(); 00114 if (callback_func_send_end != NULL) { 00115 callback_func_send_end((rootPath()).c_str(), (path()).c_str(), send_data_buf); 00116 } 00117 return; 00118 } else { 00119 send_index += writtenLen; 00120 } 00121 } else { 00122 close(); 00123 if (callback_func_send_end != NULL) { 00124 callback_func_send_end((rootPath()).c_str(), (path()).c_str(), send_data_buf); 00125 } 00126 return; 00127 } 00128 } 00129 } 00130 } 00131 00132 void SnapshotHandler::onClose() //Connection is closing 00133 { 00134 //Nothing to do 00135 } 00136
Generated on Tue Jul 12 2022 21:12:22 by 1.7.2