/** * Process a dynamic-content HTTP request * This function is only be called by the core, if HttpDynamic_InitRequest() returns nonzero. * This function processes the specified HTTP request, and send the response on the connection specified by request->uConnection. * This function must call the HttpResponse_*() functions in order to send data back to the browser. * Please refer to HttpResponse.h for more information. * @param request Pointer to all data available about the request */ int HttpDynamic_ProcessRequest(struct HttpRequest* request) { struct HttpBlob contentType,nullBlob; struct HttpBlob contentBody; void *pArgs = NULL; contentType.pData = NULL; contentType.uLength = 0; nullBlob = contentType; /* 1. Call user defined API */ contentBody.pData = g_RestContent[g_NumResource].pCbfunc(pArgs); contentBody.uLength = strlen((const char*)contentBody.pData); /* 2. Set header for HTTP Response */ if(!HttpResponse_Headers(request->uConnection, HTTP_STATUS_OK, 0, contentBody.uLength, contentType, nullBlob)) return 0; /* 3. fill the content response (if exists) */ if (contentBody.uLength != 0) { if(!HttpResponse_Content(request->uConnection, contentBody)) return 0; } return 1; }
/** * Process a static-content HTTP request * This function is called after a request was already initialized, and a Flash content entry was identified during a call to HttpStatic_InitRequest() * This function calls HttpResponse_*() to send the content data to the browser. * @param request Pointer to all data available about the request * @return nonzero if request was handled. zero if not. */ int HttpStatic_ProcessRequest(struct HttpRequest* request) { struct HttpBlob location,contentType; struct HttpBlob* content = (struct HttpBlob*)malloc(sizeof(struct HttpBlob)); unsigned long Offset = 0; SlFsFileInfo_t pFsFileInfo; UINT32 TotalLength; UINT8 HeaderFlag =0; location.pData = NULL; location.uLength = 0; contentType = location; /* if HTTP_REQUEST_FLAG_METHOD_POST==1 (i.e. it is POST) HttpResponse_CannedError() responds to client with status HTTP_STATUS_ERROR_INTERNAL POST method is not supported for static pages */ if (request->uFlags & HTTP_REQUEST_FLAG_METHOD_POST) { /* HttpResponse_CannedError responds to client with 500 ERROR_INTERNAL */ if(!HttpResponse_CannedError(request->uConnection, HTTP_STATUS_ERROR_INTERNAL)) return 0; else return 1; } //sl_FsGetInfo((unsigned char *)g_cFileName, NULL, &pFsFileInfo); sl_FsGetInfo((unsigned char *)g_cFileName, 0, &pFsFileInfo); TotalLength = (&pFsFileInfo)->FileLen; while(TotalLength > 0) { content->uLength = ((TotalLength < 1000) ? (TotalLength):(1000)); UINT8* (buffer) = (UINT8*)malloc(content->uLength); content->pData = buffer; /* if got here than it is a GET method HttpResponse_Headers() responds to client with status HTTP_STATUS_OK */ if(sl_FsRead(glFileHandle, Offset, (unsigned char *) content->pData, content->uLength) < 0) { /* call HttpResponse_CannedError responds to client with 500 ERROR_INTERNAL */ if(!HttpResponse_CannedError( request->uConnection, HTTP_STATUS_ERROR_NOT_ACCEPTED)) return 0; else return 1; } else { if(!HeaderFlag) { if(!HttpResponse_Headers( request->uConnection, HTTP_STATUS_OK, 0, TotalLength, contentType, location)) return 0; HeaderFlag = 1; } /* HttpResponse_Content() sends requested page to the client */ if(!HttpResponse_Content(request->uConnection, *content)) return 0; } TotalLength -= content->uLength; Offset += content->uLength; free(buffer); } sl_FsClose(glFileHandle,0,0,0); free(content); return 1; }