TI's CC3100 websocket camera demo with Arducam mini ov5642 and freertos. Should work with other M3's. Work in progress test demo.
http/server/HttpDynamic.cpp@1:e448e81c416f, 2015-09-11 (annotated)
- Committer:
- dflet
- Date:
- Fri Sep 11 15:38:33 2015 +0000
- Revision:
- 1:e448e81c416f
- Parent:
- 0:400d8e75a8d0
Removed some debud.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dflet | 0:400d8e75a8d0 | 1 | //***************************************************************************** |
dflet | 0:400d8e75a8d0 | 2 | // Copyright (C) 2014 Texas Instruments Incorporated |
dflet | 0:400d8e75a8d0 | 3 | // |
dflet | 0:400d8e75a8d0 | 4 | // All rights reserved. Property of Texas Instruments Incorporated. |
dflet | 0:400d8e75a8d0 | 5 | // Restricted rights to use, duplicate or disclose this code are |
dflet | 0:400d8e75a8d0 | 6 | // granted through contract. |
dflet | 0:400d8e75a8d0 | 7 | // The program may not be used without the written permission of |
dflet | 0:400d8e75a8d0 | 8 | // Texas Instruments Incorporated or against the terms and conditions |
dflet | 0:400d8e75a8d0 | 9 | // stipulated in the agreement under which this program has been supplied, |
dflet | 0:400d8e75a8d0 | 10 | // and under no circumstances can it be used with non-TI connectivity device. |
dflet | 0:400d8e75a8d0 | 11 | // |
dflet | 0:400d8e75a8d0 | 12 | //***************************************************************************** |
dflet | 0:400d8e75a8d0 | 13 | |
dflet | 0:400d8e75a8d0 | 14 | /** |
dflet | 0:400d8e75a8d0 | 15 | * @addtogroup HttpDynamic |
dflet | 0:400d8e75a8d0 | 16 | * @{ |
dflet | 0:400d8e75a8d0 | 17 | */ |
dflet | 0:400d8e75a8d0 | 18 | |
dflet | 0:400d8e75a8d0 | 19 | #include "HttpDynamic.h" |
dflet | 0:400d8e75a8d0 | 20 | #include "HttpRequest.h" |
dflet | 0:400d8e75a8d0 | 21 | #include "HttpResponse.h" |
dflet | 0:400d8e75a8d0 | 22 | #include "HttpCore.h" |
dflet | 0:400d8e75a8d0 | 23 | #include "string.h" |
dflet | 0:400d8e75a8d0 | 24 | |
dflet | 0:400d8e75a8d0 | 25 | #ifdef HTTP_CORE_ENABLE_DYNAMIC |
dflet | 0:400d8e75a8d0 | 26 | |
dflet | 0:400d8e75a8d0 | 27 | /** |
dflet | 0:400d8e75a8d0 | 28 | * @defgroup HttpDynamic Dynamic request handler module |
dflet | 0:400d8e75a8d0 | 29 | * This module implements dynamic content processing for HTTP requests. |
dflet | 0:400d8e75a8d0 | 30 | * All requests are handled by C code functions, and the response contents is returned via HttpResopnse routines |
dflet | 0:400d8e75a8d0 | 31 | * Note this module is only compiled if HTTP_CORE_ENABLE_DYNAMIC is defined in HttpConfig.h |
dflet | 0:400d8e75a8d0 | 32 | * |
dflet | 0:400d8e75a8d0 | 33 | * @{ |
dflet | 0:400d8e75a8d0 | 34 | */ |
dflet | 0:400d8e75a8d0 | 35 | |
dflet | 0:400d8e75a8d0 | 36 | Resource g_RestContent[MAX_RESOURCE]; |
dflet | 0:400d8e75a8d0 | 37 | |
dflet | 0:400d8e75a8d0 | 38 | int g_NumResource = 0; |
dflet | 0:400d8e75a8d0 | 39 | |
dflet | 0:400d8e75a8d0 | 40 | int SetResources(unsigned char method, char* pBuf, unsigned char* (*pCbRestFunc)(void *pArgs) ) |
dflet | 0:400d8e75a8d0 | 41 | { |
dflet | 0:400d8e75a8d0 | 42 | |
dflet | 0:400d8e75a8d0 | 43 | // POST is 0 and GET is 1 |
dflet | 0:400d8e75a8d0 | 44 | |
dflet | 0:400d8e75a8d0 | 45 | if(g_NumResource < MAX_RESOURCE) |
dflet | 0:400d8e75a8d0 | 46 | { |
dflet | 0:400d8e75a8d0 | 47 | g_RestContent[g_NumResource].rest_method = method; |
dflet | 0:400d8e75a8d0 | 48 | g_RestContent[g_NumResource].ResourceString = (unsigned char*)pBuf; |
dflet | 0:400d8e75a8d0 | 49 | g_RestContent[g_NumResource].pCbfunc = pCbRestFunc; |
dflet | 0:400d8e75a8d0 | 50 | g_NumResource++; |
dflet | 0:400d8e75a8d0 | 51 | } |
dflet | 0:400d8e75a8d0 | 52 | else |
dflet | 0:400d8e75a8d0 | 53 | return 0; |
dflet | 0:400d8e75a8d0 | 54 | |
dflet | 0:400d8e75a8d0 | 55 | return 1; |
dflet | 0:400d8e75a8d0 | 56 | |
dflet | 0:400d8e75a8d0 | 57 | } |
dflet | 0:400d8e75a8d0 | 58 | |
dflet | 0:400d8e75a8d0 | 59 | /** |
dflet | 0:400d8e75a8d0 | 60 | * Initialize HttpDynamic module state for a new request, and identify the request |
dflet | 0:400d8e75a8d0 | 61 | * This function must examine the specified resource string and determine whether it can commit to process this request |
dflet | 0:400d8e75a8d0 | 62 | * If this function returns nonzero, then the core will call HttpDynamic_ProcessRequest() with the rest of the request details. |
dflet | 0:400d8e75a8d0 | 63 | * @param uConnection The number of the connection. This value is guaranteed to satisfy: 0 <= uConnection < HTTP_CORE_MAX_CONNECTIONS |
dflet | 0:400d8e75a8d0 | 64 | * @param resource The resource part of the URL, as specified by the browser in the request, including any query string (and hash) |
dflet | 0:400d8e75a8d0 | 65 | * Note: The resource string exists ONLY during the call to this function. The string pointer should not be copied by this function. |
dflet | 0:400d8e75a8d0 | 66 | * @param method The HTTP method sent from the client for the resource |
dflet | 0:400d8e75a8d0 | 67 | * @return nonzero if request is to be handled by this module. zero if not. |
dflet | 0:400d8e75a8d0 | 68 | */ |
dflet | 0:400d8e75a8d0 | 69 | int HttpDynamic_InitRequest(UINT16 uConnection, struct HttpBlob resource, UINT8 method) |
dflet | 0:400d8e75a8d0 | 70 | { |
dflet | 0:400d8e75a8d0 | 71 | |
dflet | 0:400d8e75a8d0 | 72 | /* look for known resource names according to g_RestContent*/ |
dflet | 0:400d8e75a8d0 | 73 | for (g_NumResource = 0; g_NumResource < MAX_RESOURCE; g_NumResource++) |
dflet | 0:400d8e75a8d0 | 74 | { |
dflet | 0:400d8e75a8d0 | 75 | if (HttpString_nextToken((char*)g_RestContent[g_NumResource].ResourceString, strlen((const char*)g_RestContent[g_NumResource].ResourceString), resource) != NULL) |
dflet | 0:400d8e75a8d0 | 76 | break; |
dflet | 0:400d8e75a8d0 | 77 | } |
dflet | 0:400d8e75a8d0 | 78 | |
dflet | 0:400d8e75a8d0 | 79 | /* Rest resource not found */ |
dflet | 0:400d8e75a8d0 | 80 | if (g_NumResource == MAX_RESOURCE) |
dflet | 0:400d8e75a8d0 | 81 | return 0; |
dflet | 0:400d8e75a8d0 | 82 | |
dflet | 0:400d8e75a8d0 | 83 | /* Method doesn't match */ |
dflet | 0:400d8e75a8d0 | 84 | if(g_RestContent[g_NumResource].rest_method != method) |
dflet | 0:400d8e75a8d0 | 85 | return 0; |
dflet | 0:400d8e75a8d0 | 86 | |
dflet | 0:400d8e75a8d0 | 87 | |
dflet | 0:400d8e75a8d0 | 88 | return 1; |
dflet | 0:400d8e75a8d0 | 89 | } |
dflet | 0:400d8e75a8d0 | 90 | |
dflet | 0:400d8e75a8d0 | 91 | /** |
dflet | 0:400d8e75a8d0 | 92 | * Process a dynamic-content HTTP request |
dflet | 0:400d8e75a8d0 | 93 | * This function is only be called by the core, if HttpDynamic_InitRequest() returns nonzero. |
dflet | 0:400d8e75a8d0 | 94 | * This function processes the specified HTTP request, and send the response on the connection specified by request->uConnection. |
dflet | 0:400d8e75a8d0 | 95 | * This function must call the HttpResponse_*() functions in order to send data back to the browser. |
dflet | 0:400d8e75a8d0 | 96 | * Please refer to HttpResponse.h for more information. |
dflet | 0:400d8e75a8d0 | 97 | * @param request Pointer to all data available about the request |
dflet | 0:400d8e75a8d0 | 98 | */ |
dflet | 0:400d8e75a8d0 | 99 | int HttpDynamic_ProcessRequest(struct HttpRequest* request) |
dflet | 0:400d8e75a8d0 | 100 | { |
dflet | 0:400d8e75a8d0 | 101 | struct HttpBlob contentType,nullBlob; |
dflet | 0:400d8e75a8d0 | 102 | struct HttpBlob contentBody; |
dflet | 0:400d8e75a8d0 | 103 | void *pArgs = NULL; |
dflet | 0:400d8e75a8d0 | 104 | |
dflet | 0:400d8e75a8d0 | 105 | contentType.pData = NULL; |
dflet | 0:400d8e75a8d0 | 106 | contentType.uLength = 0; |
dflet | 0:400d8e75a8d0 | 107 | nullBlob = contentType; |
dflet | 0:400d8e75a8d0 | 108 | |
dflet | 0:400d8e75a8d0 | 109 | /* 1. Call user defined API */ |
dflet | 0:400d8e75a8d0 | 110 | contentBody.pData = g_RestContent[g_NumResource].pCbfunc(pArgs); |
dflet | 0:400d8e75a8d0 | 111 | contentBody.uLength = strlen((const char*)contentBody.pData); |
dflet | 0:400d8e75a8d0 | 112 | |
dflet | 0:400d8e75a8d0 | 113 | /* 2. Set header for HTTP Response */ |
dflet | 0:400d8e75a8d0 | 114 | if(!HttpResponse_Headers(request->uConnection, (UINT16)HTTP_STATUS_OK, 0, contentBody.uLength, contentType, nullBlob)) |
dflet | 0:400d8e75a8d0 | 115 | return 0; |
dflet | 0:400d8e75a8d0 | 116 | |
dflet | 0:400d8e75a8d0 | 117 | /* 3. fill the content response (if exists) */ |
dflet | 0:400d8e75a8d0 | 118 | if (contentBody.uLength != 0) |
dflet | 0:400d8e75a8d0 | 119 | { |
dflet | 0:400d8e75a8d0 | 120 | if(!HttpResponse_Content(request->uConnection, contentBody)) |
dflet | 0:400d8e75a8d0 | 121 | return 0; |
dflet | 0:400d8e75a8d0 | 122 | } |
dflet | 0:400d8e75a8d0 | 123 | |
dflet | 0:400d8e75a8d0 | 124 | return 1; |
dflet | 0:400d8e75a8d0 | 125 | } |
dflet | 0:400d8e75a8d0 | 126 | |
dflet | 0:400d8e75a8d0 | 127 | |
dflet | 0:400d8e75a8d0 | 128 | /// @} |
dflet | 0:400d8e75a8d0 | 129 | |
dflet | 0:400d8e75a8d0 | 130 | #endif // HTTP_CORE_ENABLE_DYNAMIC |
dflet | 0:400d8e75a8d0 | 131 |