A Port of TI's Webserver for the CC3000

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HttpStatic.cpp Source File

HttpStatic.cpp

00001 /*****************************************************************************
00002 *
00003 *  HttpStatic.c
00004 *  Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
00005 *
00006 *  Redistribution and use in source and binary forms, with or without
00007 *  modification, are permitted provided that the following conditions
00008 *  are met:
00009 *
00010 *    Redistributions of source code must retain the above copyright
00011 *    notice, this list of conditions and the following disclaimer.
00012 *
00013 *    Redistributions in binary form must reproduce the above copyright
00014 *    notice, this list of conditions and the following disclaimer in the
00015 *    documentation and/or other materials provided with the   
00016 *    distribution.
00017 *
00018 *    Neither the name of Texas Instruments Incorporated nor the names of
00019 *    its contributors may be used to endorse or promote products derived
00020 *    from this software without specific prior written permission.
00021 *
00022 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00023 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
00024 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00025 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
00026 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
00027 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
00028 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00029 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00030 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00031 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
00032 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 *
00034 *****************************************************************************/
00035 #include "HttpStatic.h"
00036 
00037 #ifdef HTTP_CORE_ENABLE_STATIC
00038 
00039 #include "HttpRequest.h"
00040 #include "HttpResponse.h"
00041 #include "HttpCore.h"
00042 #include "FlashDB.h"
00043 
00044 /** 
00045  * @addtogroup HttpStatic
00046  * @{
00047  */
00048 
00049 /** 
00050  * @defgroup HttpStatic Static request handler module
00051  * This module implements static content processing for HTTP requests.
00052  * All requests are handled by looking up the URL's resource in the flash database, and returning the content in the response.
00053  * Note this module is only compiled if HTTP_CORE_ENABLE_STATIC is defined in HttpConfig.h
00054  *
00055  * @{
00056  */
00057 
00058 
00059 struct FlashDBContent pFlashDBContent[HTTP_CORE_MAX_CONNECTIONS];
00060 
00061 /**
00062  * Initialize HttpStatic module state for a new request, and identify the request
00063  * This function examines the specified resource string, and looks it up in the Flash Database.
00064  * Note: During FlashDB lookup, ignore the query part (?) and anchor part (#) of URL
00065  * If found, it commits to process this request by returning nonzero. Otherwise it returns zero.
00066  * @param uConnection The number of the connection. This value is guaranteed to satisfy: 0 <= uConnection < HTTP_CORE_MAX_CONNECTIONS
00067  * @param resource The resource part of the URL, as specified by the browser in the request, including any query string (and hash). 
00068  *                 Note: The resource string exists ONLY during the call to this function. The string pointer should not be copied by this function.
00069  * @return nonzero if request is to be handled by this module. zero if not.
00070  */
00071 int HttpStatic_InitRequest(uint16 uConnection, struct HttpBlob resource)
00072 {
00073     if (FlashDB_FindContent(resource, &pFlashDBContent[uConnection]) == 0)
00074         return 0;
00075 
00076     return 1;
00077 }
00078 
00079 /**
00080  * Process a static-content HTTP request
00081  * This function is called after a request was already initialized, and a FlashDB content entry was identified during a call to HttpStatic_InitRequest()
00082  * This function calls HttpResponse_*() to send the content data to the browser.
00083  * @param request Pointer to all data available about the request
00084  * @return nonzero if request was handled. zero if not.
00085  */
00086 void HttpStatic_ProcessRequest(struct HttpRequest* request)
00087 {
00088     struct HttpBlob contentType, nullBlob = {0, NULL};
00089     
00090     /*  if HTTP_REQUEST_FLAG_METHOD_POST==1 (i.e. it is POST)
00091         HttpResponse_CannedError() responds to client with status HTTP_STATUS_ERROR_INTERNAL
00092         POST method is not supported for static pages   */
00093     if (request->uFlags & HTTP_REQUEST_FLAG_METHOD_POST)
00094     {
00095          /* HttpResponse_CannedError responds to client with 500 ERROR_INTERNAL  */
00096          HttpResponse_CannedError(request->uConnection, HTTP_STATUS_ERROR_INTERNAL);
00097          return;
00098     }
00099 
00100     /* if HTTP_REQUEST_FLAG_ACCEPT_GZIP is not set and FLASHDB_FLAG_COMPRESSED is set then 
00101             HttpResponse_CannedError() responds to client with status HTTP_STATUS_ERROR_NOT_ACCEPTED    */
00102      if ((((char)(request->uFlags) & (char)HTTP_REQUEST_FLAG_ACCEPT_GZIP) == 0) && (((char)(pFlashDBContent[request->uConnection].uFlags) & (char)FLASHDB_FLAG_COMPRESSED)))
00103      {
00104          /* call HttpResponse_CannedError responds to client with 500 ERROR_INTERNAL  */
00105          HttpResponse_CannedError(request->uConnection, HTTP_STATUS_ERROR_NOT_ACCEPTED);
00106          return;
00107     }
00108      /* if HTTP_REQUEST_FLAG_AUTHENTICATED is not set and FLASHDB_FLAG_REQUIRE_AUTH is set then 
00109             HttpResponse_CannedError() responds to client with status HTTP_STATUS_ERROR_UNAUTHORIZED    */
00110     if (((request->uFlags & HTTP_REQUEST_FLAG_AUTHENTICATED) == 0))
00111     {
00112          /* HttpResponse_CannedError responds to client with 500 ERROR_INTERNAL  */
00113          HttpResponse_CannedError(request->uConnection, HTTP_STATUS_ERROR_UNAUTHORIZED);
00114          return;
00115     }
00116 
00117     /* if got here than it is a GET method
00118         HttpResponse_Headers() responds to client with status HTTP_STATUS_OK */
00119     FlashDB_FindContentType(pFlashDBContent[request->uConnection].uContentType, &contentType);
00120     HttpResponse_Headers(request->uConnection, HTTP_STATUS_OK, pFlashDBContent[request->uConnection].uFlags,  pFlashDBContent[request->uConnection].contentBlob.uLength, contentType, nullBlob);
00121     /* HttpResponse_Content() sends requested page to the client */
00122     HttpResponse_Content(request->uConnection, pFlashDBContent[request->uConnection].contentBlob);
00123     
00124 }
00125 
00126 /// @}
00127 
00128 #endif // HTTP_CORE_ENABLE_STATIC
00129