David Fletcher
/
CC3000WebServer
A Port of TI's Webserver for the CC3000
WebServer/HttpStatic.cpp@2:e6a185df9e4c, 2013-09-16 (annotated)
- Committer:
- dflet
- Date:
- Mon Sep 16 18:37:14 2013 +0000
- Revision:
- 2:e6a185df9e4c
- Parent:
- 0:6ad60d78b315
ADC and Leds now work on board and config.html page.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dflet | 0:6ad60d78b315 | 1 | /***************************************************************************** |
dflet | 0:6ad60d78b315 | 2 | * |
dflet | 0:6ad60d78b315 | 3 | * HttpStatic.c |
dflet | 0:6ad60d78b315 | 4 | * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ |
dflet | 0:6ad60d78b315 | 5 | * |
dflet | 0:6ad60d78b315 | 6 | * Redistribution and use in source and binary forms, with or without |
dflet | 0:6ad60d78b315 | 7 | * modification, are permitted provided that the following conditions |
dflet | 0:6ad60d78b315 | 8 | * are met: |
dflet | 0:6ad60d78b315 | 9 | * |
dflet | 0:6ad60d78b315 | 10 | * Redistributions of source code must retain the above copyright |
dflet | 0:6ad60d78b315 | 11 | * notice, this list of conditions and the following disclaimer. |
dflet | 0:6ad60d78b315 | 12 | * |
dflet | 0:6ad60d78b315 | 13 | * Redistributions in binary form must reproduce the above copyright |
dflet | 0:6ad60d78b315 | 14 | * notice, this list of conditions and the following disclaimer in the |
dflet | 0:6ad60d78b315 | 15 | * documentation and/or other materials provided with the |
dflet | 0:6ad60d78b315 | 16 | * distribution. |
dflet | 0:6ad60d78b315 | 17 | * |
dflet | 0:6ad60d78b315 | 18 | * Neither the name of Texas Instruments Incorporated nor the names of |
dflet | 0:6ad60d78b315 | 19 | * its contributors may be used to endorse or promote products derived |
dflet | 0:6ad60d78b315 | 20 | * from this software without specific prior written permission. |
dflet | 0:6ad60d78b315 | 21 | * |
dflet | 0:6ad60d78b315 | 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
dflet | 0:6ad60d78b315 | 23 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
dflet | 0:6ad60d78b315 | 24 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
dflet | 0:6ad60d78b315 | 25 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
dflet | 0:6ad60d78b315 | 26 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
dflet | 0:6ad60d78b315 | 27 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
dflet | 0:6ad60d78b315 | 28 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
dflet | 0:6ad60d78b315 | 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
dflet | 0:6ad60d78b315 | 30 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
dflet | 0:6ad60d78b315 | 31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
dflet | 0:6ad60d78b315 | 32 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
dflet | 0:6ad60d78b315 | 33 | * |
dflet | 0:6ad60d78b315 | 34 | *****************************************************************************/ |
dflet | 0:6ad60d78b315 | 35 | #include "HttpStatic.h" |
dflet | 0:6ad60d78b315 | 36 | |
dflet | 0:6ad60d78b315 | 37 | #ifdef HTTP_CORE_ENABLE_STATIC |
dflet | 0:6ad60d78b315 | 38 | |
dflet | 0:6ad60d78b315 | 39 | #include "HttpRequest.h" |
dflet | 0:6ad60d78b315 | 40 | #include "HttpResponse.h" |
dflet | 0:6ad60d78b315 | 41 | #include "HttpCore.h" |
dflet | 0:6ad60d78b315 | 42 | #include "FlashDB.h" |
dflet | 0:6ad60d78b315 | 43 | |
dflet | 0:6ad60d78b315 | 44 | /** |
dflet | 0:6ad60d78b315 | 45 | * @addtogroup HttpStatic |
dflet | 0:6ad60d78b315 | 46 | * @{ |
dflet | 0:6ad60d78b315 | 47 | */ |
dflet | 0:6ad60d78b315 | 48 | |
dflet | 0:6ad60d78b315 | 49 | /** |
dflet | 0:6ad60d78b315 | 50 | * @defgroup HttpStatic Static request handler module |
dflet | 0:6ad60d78b315 | 51 | * This module implements static content processing for HTTP requests. |
dflet | 0:6ad60d78b315 | 52 | * All requests are handled by looking up the URL's resource in the flash database, and returning the content in the response. |
dflet | 0:6ad60d78b315 | 53 | * Note this module is only compiled if HTTP_CORE_ENABLE_STATIC is defined in HttpConfig.h |
dflet | 0:6ad60d78b315 | 54 | * |
dflet | 0:6ad60d78b315 | 55 | * @{ |
dflet | 0:6ad60d78b315 | 56 | */ |
dflet | 0:6ad60d78b315 | 57 | |
dflet | 0:6ad60d78b315 | 58 | |
dflet | 0:6ad60d78b315 | 59 | struct FlashDBContent pFlashDBContent[HTTP_CORE_MAX_CONNECTIONS]; |
dflet | 0:6ad60d78b315 | 60 | |
dflet | 0:6ad60d78b315 | 61 | /** |
dflet | 0:6ad60d78b315 | 62 | * Initialize HttpStatic module state for a new request, and identify the request |
dflet | 0:6ad60d78b315 | 63 | * This function examines the specified resource string, and looks it up in the Flash Database. |
dflet | 0:6ad60d78b315 | 64 | * Note: During FlashDB lookup, ignore the query part (?) and anchor part (#) of URL |
dflet | 0:6ad60d78b315 | 65 | * If found, it commits to process this request by returning nonzero. Otherwise it returns zero. |
dflet | 0:6ad60d78b315 | 66 | * @param uConnection The number of the connection. This value is guaranteed to satisfy: 0 <= uConnection < HTTP_CORE_MAX_CONNECTIONS |
dflet | 0:6ad60d78b315 | 67 | * @param resource The resource part of the URL, as specified by the browser in the request, including any query string (and hash). |
dflet | 0:6ad60d78b315 | 68 | * Note: The resource string exists ONLY during the call to this function. The string pointer should not be copied by this function. |
dflet | 0:6ad60d78b315 | 69 | * @return nonzero if request is to be handled by this module. zero if not. |
dflet | 0:6ad60d78b315 | 70 | */ |
dflet | 0:6ad60d78b315 | 71 | int HttpStatic_InitRequest(uint16 uConnection, struct HttpBlob resource) |
dflet | 0:6ad60d78b315 | 72 | { |
dflet | 0:6ad60d78b315 | 73 | if (FlashDB_FindContent(resource, &pFlashDBContent[uConnection]) == 0) |
dflet | 0:6ad60d78b315 | 74 | return 0; |
dflet | 0:6ad60d78b315 | 75 | |
dflet | 0:6ad60d78b315 | 76 | return 1; |
dflet | 0:6ad60d78b315 | 77 | } |
dflet | 0:6ad60d78b315 | 78 | |
dflet | 0:6ad60d78b315 | 79 | /** |
dflet | 0:6ad60d78b315 | 80 | * Process a static-content HTTP request |
dflet | 0:6ad60d78b315 | 81 | * This function is called after a request was already initialized, and a FlashDB content entry was identified during a call to HttpStatic_InitRequest() |
dflet | 0:6ad60d78b315 | 82 | * This function calls HttpResponse_*() to send the content data to the browser. |
dflet | 0:6ad60d78b315 | 83 | * @param request Pointer to all data available about the request |
dflet | 0:6ad60d78b315 | 84 | * @return nonzero if request was handled. zero if not. |
dflet | 0:6ad60d78b315 | 85 | */ |
dflet | 0:6ad60d78b315 | 86 | void HttpStatic_ProcessRequest(struct HttpRequest* request) |
dflet | 0:6ad60d78b315 | 87 | { |
dflet | 0:6ad60d78b315 | 88 | struct HttpBlob contentType, nullBlob = {0, NULL}; |
dflet | 0:6ad60d78b315 | 89 | |
dflet | 0:6ad60d78b315 | 90 | /* if HTTP_REQUEST_FLAG_METHOD_POST==1 (i.e. it is POST) |
dflet | 0:6ad60d78b315 | 91 | HttpResponse_CannedError() responds to client with status HTTP_STATUS_ERROR_INTERNAL |
dflet | 0:6ad60d78b315 | 92 | POST method is not supported for static pages */ |
dflet | 0:6ad60d78b315 | 93 | if (request->uFlags & HTTP_REQUEST_FLAG_METHOD_POST) |
dflet | 0:6ad60d78b315 | 94 | { |
dflet | 0:6ad60d78b315 | 95 | /* HttpResponse_CannedError responds to client with 500 ERROR_INTERNAL */ |
dflet | 0:6ad60d78b315 | 96 | HttpResponse_CannedError(request->uConnection, HTTP_STATUS_ERROR_INTERNAL); |
dflet | 0:6ad60d78b315 | 97 | return; |
dflet | 0:6ad60d78b315 | 98 | } |
dflet | 0:6ad60d78b315 | 99 | |
dflet | 0:6ad60d78b315 | 100 | /* if HTTP_REQUEST_FLAG_ACCEPT_GZIP is not set and FLASHDB_FLAG_COMPRESSED is set then |
dflet | 0:6ad60d78b315 | 101 | HttpResponse_CannedError() responds to client with status HTTP_STATUS_ERROR_NOT_ACCEPTED */ |
dflet | 0:6ad60d78b315 | 102 | if ((((char)(request->uFlags) & (char)HTTP_REQUEST_FLAG_ACCEPT_GZIP) == 0) && (((char)(pFlashDBContent[request->uConnection].uFlags) & (char)FLASHDB_FLAG_COMPRESSED))) |
dflet | 0:6ad60d78b315 | 103 | { |
dflet | 0:6ad60d78b315 | 104 | /* call HttpResponse_CannedError responds to client with 500 ERROR_INTERNAL */ |
dflet | 0:6ad60d78b315 | 105 | HttpResponse_CannedError(request->uConnection, HTTP_STATUS_ERROR_NOT_ACCEPTED); |
dflet | 0:6ad60d78b315 | 106 | return; |
dflet | 0:6ad60d78b315 | 107 | } |
dflet | 0:6ad60d78b315 | 108 | /* if HTTP_REQUEST_FLAG_AUTHENTICATED is not set and FLASHDB_FLAG_REQUIRE_AUTH is set then |
dflet | 0:6ad60d78b315 | 109 | HttpResponse_CannedError() responds to client with status HTTP_STATUS_ERROR_UNAUTHORIZED */ |
dflet | 0:6ad60d78b315 | 110 | if (((request->uFlags & HTTP_REQUEST_FLAG_AUTHENTICATED) == 0)) |
dflet | 0:6ad60d78b315 | 111 | { |
dflet | 0:6ad60d78b315 | 112 | /* HttpResponse_CannedError responds to client with 500 ERROR_INTERNAL */ |
dflet | 0:6ad60d78b315 | 113 | HttpResponse_CannedError(request->uConnection, HTTP_STATUS_ERROR_UNAUTHORIZED); |
dflet | 0:6ad60d78b315 | 114 | return; |
dflet | 0:6ad60d78b315 | 115 | } |
dflet | 0:6ad60d78b315 | 116 | |
dflet | 0:6ad60d78b315 | 117 | /* if got here than it is a GET method |
dflet | 0:6ad60d78b315 | 118 | HttpResponse_Headers() responds to client with status HTTP_STATUS_OK */ |
dflet | 0:6ad60d78b315 | 119 | FlashDB_FindContentType(pFlashDBContent[request->uConnection].uContentType, &contentType); |
dflet | 0:6ad60d78b315 | 120 | HttpResponse_Headers(request->uConnection, HTTP_STATUS_OK, pFlashDBContent[request->uConnection].uFlags, pFlashDBContent[request->uConnection].contentBlob.uLength, contentType, nullBlob); |
dflet | 0:6ad60d78b315 | 121 | /* HttpResponse_Content() sends requested page to the client */ |
dflet | 0:6ad60d78b315 | 122 | HttpResponse_Content(request->uConnection, pFlashDBContent[request->uConnection].contentBlob); |
dflet | 0:6ad60d78b315 | 123 | |
dflet | 0:6ad60d78b315 | 124 | } |
dflet | 0:6ad60d78b315 | 125 | |
dflet | 0:6ad60d78b315 | 126 | /// @} |
dflet | 0:6ad60d78b315 | 127 | |
dflet | 0:6ad60d78b315 | 128 | #endif // HTTP_CORE_ENABLE_STATIC |
dflet | 0:6ad60d78b315 | 129 |