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