Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
HttpStatic.cpp
00001 //***************************************************************************** 00002 // Copyright (C) 2014 Texas Instruments Incorporated 00003 // 00004 // All rights reserved. Property of Texas Instruments Incorporated. 00005 // Restricted rights to use, duplicate or disclose this code are 00006 // granted through contract. 00007 // The program may not be used without the written permission of 00008 // Texas Instruments Incorporated or against the terms and conditions 00009 // stipulated in the agreement under which this program has been supplied, 00010 // and under no circumstances can it be used with non-TI connectivity device. 00011 // 00012 //***************************************************************************** 00013 00014 #include "HttpStatic.h" 00015 #include <stdlib.h> 00016 00017 #ifdef HTTP_CORE_ENABLE_STATIC 00018 00019 #include "HttpRequest.h" 00020 #include "HttpResponse.h" 00021 #include "HttpCore.h" 00022 #include "cc3100.h" 00023 #include "cc3100_fs.h" 00024 #include "HttpDebug.h" 00025 #include "myBoardInit.h" 00026 #include "osi.h" 00027 00028 using namespace mbed_cc3100; 00029 00030 //cc3100 _cc3100Module(NC, NC, PE_5, PE_4, PE_6, SPI(PB_5, PB_4, PB_3));//Seeed_Arch_Max irq, nHib, cs, mosi, miso, sck 00031 cc3100 _cc3100Module(NC, NC, PD_12, PD_13, PD_11, SPI(PC_3, PC_2, PB_10));//Seeed_Arch_Max irq, nHib, cs, mosi, miso, sck 00032 00033 00034 /** 00035 * @addtogroup HttpStatic 00036 * @{ 00037 */ 00038 00039 /** 00040 * @defgroup HttpStatic Static request handler module 00041 * This module implements static content processing for HTTP requests. 00042 * All requests are handled by looking up the URL's resource in the flash database, and returning the content in the response. 00043 * Note this module is only compiled if HTTP_CORE_ENABLE_STATIC is defined in HttpConfig.h 00044 * 00045 * @{ 00046 */ 00047 00048 #define FILE_NAME_SIZE_MAX (40) 00049 00050 //Store File Name from HTTP Header 00051 char g_cFileName[FILE_NAME_SIZE_MAX]; //String storing filename 00052 int32_t glFileHandle; // file handle 00053 00054 /** 00055 * Initialize HttpStatic module state for a new request, and identify the request 00056 * This function examines the specified resource string, and looks it up in the Flash Database. 00057 * If found, it commits to process this request by returning nonzero. Otherwise it returns zero. 00058 * @param uConnection The number of the connection. This value is guaranteed to satisfy: 0 <= uConnection < HTTP_CORE_MAX_CONNECTIONS 00059 * @param resource The resource part of the URL, as specified by the browser in the request, including any query string (and hash). 00060 * Note: The resource string exists ONLY during the call to this function. The string pointer should not be copied by this function. 00061 * @return nonzero if request is to be handled by this module. zero if not. 00062 */ 00063 int HttpStatic_InitRequest(UINT16 uConnection, struct HttpBlob resource) 00064 { 00065 char *pcWWWFsDir = "www"; 00066 memset(g_cFileName,'\0',40); 00067 00068 if(resource.uLength ==1 && *(resource.pData)=='/') 00069 { 00070 strcpy(g_cFileName,"www/main.html"); 00071 } 00072 else 00073 { 00074 strcpy(g_cFileName,pcWWWFsDir); 00075 strncat(g_cFileName,(char*)resource.pData,resource.uLength); 00076 } 00077 00078 if(_cc3100Module._fs.sl_FsOpen((unsigned char*)g_cFileName,FS_MODE_OPEN_READ,NULL,&glFileHandle)<0) 00079 return 0; 00080 else 00081 { 00082 return 1; 00083 } 00084 } 00085 00086 /** 00087 * Process a static-content HTTP request 00088 * This function is called after a request was already initialized, and a Flash content entry was identified during a call to HttpStatic_InitRequest() 00089 * This function calls HttpResponse_*() to send the content data to the browser. 00090 * @param request Pointer to all data available about the request 00091 * @return nonzero if request was handled. zero if not. 00092 */ 00093 int HttpStatic_ProcessRequest(struct HttpRequest* request) 00094 { 00095 struct HttpBlob location,contentType; 00096 struct HttpBlob* content = (struct HttpBlob*)malloc(sizeof(struct HttpBlob)); 00097 unsigned int Offset = 0; 00098 SlFsFileInfo_t pFsFileInfo; 00099 UINT32 TotalLength; 00100 UINT8 HeaderFlag =0; 00101 UINT8 bRetVal = 1; 00102 UINT8 *buffer1 = NULL; 00103 00104 00105 if(content == NULL) 00106 { 00107 HttpDebug("content = NULL error\n\r"); 00108 return 0; 00109 } 00110 location.pData = NULL; 00111 location.uLength = 0; 00112 contentType = location; 00113 00114 /* if HTTP_REQUEST_FLAG_METHOD_POST==1 (i.e. it is POST) 00115 HttpResponse_CannedError() responds to client with status HTTP_STATUS_ERROR_INTERNAL 00116 POST method is not supported for static pages */ 00117 if (request->uFlags & HTTP_REQUEST_FLAG_METHOD_POST) 00118 { 00119 /* HttpResponse_CannedError responds to client with 500 ERROR_INTERNAL */ 00120 if(!HttpResponse_CannedError(request->uConnection, HTTP_STATUS_ERROR_INTERNAL)) 00121 { 00122 HttpDebug("HttpResponse_CannedError \n\r"); 00123 bRetVal = 0; 00124 goto end; 00125 } 00126 else 00127 { 00128 bRetVal = 1; 00129 goto end; 00130 } 00131 } 00132 00133 _cc3100Module._fs.sl_FsGetInfo((unsigned char *)g_cFileName, NULL, &pFsFileInfo); 00134 TotalLength = (&pFsFileInfo)->FileLen; 00135 00136 while(TotalLength > 0) 00137 { 00138 00139 // HttpDebug("TotalLength = %d\n\r",TotalLength); 00140 content->uLength = ((TotalLength < 1000) ? (TotalLength):(1000)); 00141 buffer1 = (uint8_t*)realloc(buffer1, content->uLength); 00142 if(buffer1 == NULL) 00143 { 00144 HttpDebug("buffer1 = NULL Error \n\r"); 00145 bRetVal = 0; 00146 goto end; 00147 } 00148 content->pData = buffer1; 00149 00150 /* if got here than it is a GET method 00151 HttpResponse_Headers() responds to client with status HTTP_STATUS_OK */ 00152 if(_cc3100Module._fs.sl_FsRead(glFileHandle, Offset, (unsigned char *) content->pData, content->uLength) < 0) 00153 { 00154 /* call HttpResponse_CannedError responds to client with 500 ERROR_INTERNAL */ 00155 if(!HttpResponse_CannedError(request->uConnection, HTTP_STATUS_ERROR_NOT_ACCEPTED)) 00156 { 00157 HttpDebug("500 ERROR_INTERNAL Error \n\r"); 00158 bRetVal = 0; 00159 goto end; 00160 } 00161 else 00162 { 00163 bRetVal = 1; 00164 goto end; 00165 } 00166 } 00167 else 00168 { 00169 if(!HeaderFlag) 00170 { 00171 if(!HttpResponse_Headers(request->uConnection, (uint16_t)HTTP_STATUS_OK, NULL,TotalLength, contentType, location)) 00172 { 00173 HttpDebug("HeaderFlag Error \n\r"); 00174 bRetVal = 0; 00175 goto end; 00176 } 00177 HeaderFlag = 1; 00178 } 00179 00180 /* HttpResponse_Content() sends requested page to the client */ 00181 if(!HttpResponse_Content(request->uConnection, *content)) 00182 { 00183 HttpDebug("HttpResponse_Content Error \n\r"); 00184 bRetVal = 0; 00185 goto end; 00186 } 00187 } 00188 00189 TotalLength -= content->uLength; 00190 Offset += content->uLength; 00191 } 00192 00193 _cc3100Module._fs.sl_FsClose(glFileHandle,0,0,0); 00194 end: 00195 if(buffer1 != NULL) 00196 { 00197 free(buffer1); 00198 } 00199 free(content); 00200 00201 return bRetVal; 00202 } 00203 00204 /// @} 00205 00206 #endif // HTTP_CORE_ENABLE_STATIC 00207
Generated on Tue Jul 12 2022 22:22:38 by
1.7.2