Dependencies: FatFileSystem mbed WeatherMeters SDFileSystem
HTTPServer.h@0:1a61c61d0845, 2012-04-03 (annotated)
- Committer:
- dcoban
- Date:
- Tue Apr 03 18:43:13 2012 +0000
- Revision:
- 0:1a61c61d0845
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dcoban | 0:1a61c61d0845 | 1 | /* Copyright 2011 Adam Green (http://mbed.org/users/AdamGreen/) |
dcoban | 0:1a61c61d0845 | 2 | |
dcoban | 0:1a61c61d0845 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); |
dcoban | 0:1a61c61d0845 | 4 | you may not use this file except in compliance with the License. |
dcoban | 0:1a61c61d0845 | 5 | You may obtain a copy of the License at |
dcoban | 0:1a61c61d0845 | 6 | |
dcoban | 0:1a61c61d0845 | 7 | http://www.apache.org/licenses/LICENSE-2.0 |
dcoban | 0:1a61c61d0845 | 8 | |
dcoban | 0:1a61c61d0845 | 9 | Unless required by applicable law or agreed to in writing, software |
dcoban | 0:1a61c61d0845 | 10 | distributed under the License is distributed on an "AS IS" BASIS, |
dcoban | 0:1a61c61d0845 | 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
dcoban | 0:1a61c61d0845 | 12 | See the License for the specific language governing permissions and |
dcoban | 0:1a61c61d0845 | 13 | limitations under the License. |
dcoban | 0:1a61c61d0845 | 14 | */ |
dcoban | 0:1a61c61d0845 | 15 | /* Header file for HTTP Server functionality. */ |
dcoban | 0:1a61c61d0845 | 16 | #ifndef HTTPSERVER_H_ |
dcoban | 0:1a61c61d0845 | 17 | #define HTTPSERVER_H_ |
dcoban | 0:1a61c61d0845 | 18 | |
dcoban | 0:1a61c61d0845 | 19 | #include <mbed.h> |
dcoban | 0:1a61c61d0845 | 20 | #include "lwip/tcp.h" |
dcoban | 0:1a61c61d0845 | 21 | |
dcoban | 0:1a61c61d0845 | 22 | |
dcoban | 0:1a61c61d0845 | 23 | |
dcoban | 0:1a61c61d0845 | 24 | // UNDONE: Make protected member of CHTTPServer object? |
dcoban | 0:1a61c61d0845 | 25 | /** Structure used to buffer data being read in from content files and then |
dcoban | 0:1a61c61d0845 | 26 | written out the TCP/IP connections. |
dcoban | 0:1a61c61d0845 | 27 | */ |
dcoban | 0:1a61c61d0845 | 28 | struct SBuffer |
dcoban | 0:1a61c61d0845 | 29 | { |
dcoban | 0:1a61c61d0845 | 30 | char* pWrite; /**< Pointer into Data where the next tcp_write() should begin. There are BytesToWrite valid bytes left in the Data array to be sent. */ |
dcoban | 0:1a61c61d0845 | 31 | // UNDONE: These can be shorts since mbed only has a total of 64k anyway. |
dcoban | 0:1a61c61d0845 | 32 | unsigned int BytesToWrite; /**< Number of bytes in buffer left to be written to outbound connection. */ |
dcoban | 0:1a61c61d0845 | 33 | unsigned int UnacknowledgedBytes; /**< Number of bytes outstanding on network waiting for acknowledgment. */ |
dcoban | 0:1a61c61d0845 | 34 | char Data[TCP_SND_BUF]; /**< The actual buffer holding data to be sent to remote client. */ |
dcoban | 0:1a61c61d0845 | 35 | }; |
dcoban | 0:1a61c61d0845 | 36 | |
dcoban | 0:1a61c61d0845 | 37 | |
dcoban | 0:1a61c61d0845 | 38 | |
dcoban | 0:1a61c61d0845 | 39 | |
dcoban | 0:1a61c61d0845 | 40 | |
dcoban | 0:1a61c61d0845 | 41 | // ***************** |
dcoban | 0:1a61c61d0845 | 42 | // * C++ Interface * |
dcoban | 0:1a61c61d0845 | 43 | // ***************** |
dcoban | 0:1a61c61d0845 | 44 | /** Interface to be returned from an application to provide callbacks for a |
dcoban | 0:1a61c61d0845 | 45 | specific HTTP request. |
dcoban | 0:1a61c61d0845 | 46 | */ |
dcoban | 0:1a61c61d0845 | 47 | class IHTTPRequestHandlerContext |
dcoban | 0:1a61c61d0845 | 48 | { |
dcoban | 0:1a61c61d0845 | 49 | public: |
dcoban | 0:1a61c61d0845 | 50 | /** Called by the CHTTPServer object before any WriteRequestHeader() calls |
dcoban | 0:1a61c61d0845 | 51 | to let the application to know to expect subsequent |
dcoban | 0:1a61c61d0845 | 52 | WriteRequestHeader() calls. |
dcoban | 0:1a61c61d0845 | 53 | |
dcoban | 0:1a61c61d0845 | 54 | @returns 0 if the application isn't interested in receiving subsequent |
dcoban | 0:1a61c61d0845 | 55 | WriteRequestHeader() and EndRequestHeaders() callbacks. |
dcoban | 0:1a61c61d0845 | 56 | Returns non-zero value to receive headers. |
dcoban | 0:1a61c61d0845 | 57 | */ |
dcoban | 0:1a61c61d0845 | 58 | virtual int BeginRequestHeaders() = 0; |
dcoban | 0:1a61c61d0845 | 59 | |
dcoban | 0:1a61c61d0845 | 60 | /** Called by CHTTPServer for each HTTP request header sent by the HTTP |
dcoban | 0:1a61c61d0845 | 61 | client. The server won't issue this callback if 0 was previously |
dcoban | 0:1a61c61d0845 | 62 | returned by the application from the BeginRequestHeaders() method. |
dcoban | 0:1a61c61d0845 | 63 | |
dcoban | 0:1a61c61d0845 | 64 | @param pHeaderName is the name of this header field. If this string |
dcoban | 0:1a61c61d0845 | 65 | is empty then this header line is a continuation of the previous |
dcoban | 0:1a61c61d0845 | 66 | header line which did have a field name. The string |
dcoban | 0:1a61c61d0845 | 67 | pointed to by this parameter has a short lifetime and is only |
dcoban | 0:1a61c61d0845 | 68 | valid during the duration of this call. |
dcoban | 0:1a61c61d0845 | 69 | @param pHeaderValue is the string value of this header field. The |
dcoban | 0:1a61c61d0845 | 70 | string pointed to by this parameter has a short lifetime and is |
dcoban | 0:1a61c61d0845 | 71 | only valid during the duration of this call. |
dcoban | 0:1a61c61d0845 | 72 | */ |
dcoban | 0:1a61c61d0845 | 73 | virtual void WriteRequestHeader(const char* pHeaderName, |
dcoban | 0:1a61c61d0845 | 74 | const char* pHeaderValue) = 0; |
dcoban | 0:1a61c61d0845 | 75 | |
dcoban | 0:1a61c61d0845 | 76 | /** Called by the CHTTPServer object after it has completed all of the |
dcoban | 0:1a61c61d0845 | 77 | necessary WriteRequestHeader() calls to enumerate the headers of the |
dcoban | 0:1a61c61d0845 | 78 | HTTP client's request. |
dcoban | 0:1a61c61d0845 | 79 | */ |
dcoban | 0:1a61c61d0845 | 80 | virtual void EndRequestHeaders() = 0; |
dcoban | 0:1a61c61d0845 | 81 | |
dcoban | 0:1a61c61d0845 | 82 | |
dcoban | 0:1a61c61d0845 | 83 | /** Called by CHTTPServer to let the application know that it should expect |
dcoban | 0:1a61c61d0845 | 84 | subsequent WriteRequestContentBlock() calls for processing of the |
dcoban | 0:1a61c61d0845 | 85 | request content as it arrives on the TCP/IP socket. |
dcoban | 0:1a61c61d0845 | 86 | |
dcoban | 0:1a61c61d0845 | 87 | Only POST requests from a client should include such content data. |
dcoban | 0:1a61c61d0845 | 88 | |
dcoban | 0:1a61c61d0845 | 89 | @param ContentSize is the size of the request content to be sent from |
dcoban | 0:1a61c61d0845 | 90 | the HTTP client. This value indicates the total number of bytes |
dcoban | 0:1a61c61d0845 | 91 | that should result from all of the WriteRequestContentBlock() |
dcoban | 0:1a61c61d0845 | 92 | calls to follow. |
dcoban | 0:1a61c61d0845 | 93 | |
dcoban | 0:1a61c61d0845 | 94 | @returns 0 if the application isn't interested in receiving subsequent |
dcoban | 0:1a61c61d0845 | 95 | WriteRequestContentBlock() and EndRequestContent() callbacks. |
dcoban | 0:1a61c61d0845 | 96 | Returns non-zero value to receive request content data. |
dcoban | 0:1a61c61d0845 | 97 | */ |
dcoban | 0:1a61c61d0845 | 98 | virtual int BeginRequestContent(size_t ContentSize) = 0; |
dcoban | 0:1a61c61d0845 | 99 | |
dcoban | 0:1a61c61d0845 | 100 | /** Called for each block of request data received from the HTTP client. |
dcoban | 0:1a61c61d0845 | 101 | The server won't issue this callback if the application returned 0 from |
dcoban | 0:1a61c61d0845 | 102 | the BeginRequestContent() method. |
dcoban | 0:1a61c61d0845 | 103 | |
dcoban | 0:1a61c61d0845 | 104 | @param pBlockBuffer is a pointer to the block of request content data |
dcoban | 0:1a61c61d0845 | 105 | most recently received from the HTTP client. |
dcoban | 0:1a61c61d0845 | 106 | @param BlockBufferSize is the number of bytes contained in |
dcoban | 0:1a61c61d0845 | 107 | pBlockBuffer. |
dcoban | 0:1a61c61d0845 | 108 | */ |
dcoban | 0:1a61c61d0845 | 109 | virtual void WriteRequestContentBlock(const void* pBlockBuffer, |
dcoban | 0:1a61c61d0845 | 110 | size_t BlockBufferSize) = 0; |
dcoban | 0:1a61c61d0845 | 111 | |
dcoban | 0:1a61c61d0845 | 112 | /** Called by CHTTPServer after it has completed all of the |
dcoban | 0:1a61c61d0845 | 113 | WriteRequestContentBlock() calls for processing of the request content |
dcoban | 0:1a61c61d0845 | 114 | data sent from the HTTP client. |
dcoban | 0:1a61c61d0845 | 115 | */ |
dcoban | 0:1a61c61d0845 | 116 | virtual void EndRequestContent() = 0; |
dcoban | 0:1a61c61d0845 | 117 | |
dcoban | 0:1a61c61d0845 | 118 | |
dcoban | 0:1a61c61d0845 | 119 | /** Called by CHTTPServer object to get the status line from the |
dcoban | 0:1a61c61d0845 | 120 | application to be returned to the HTTP client for this request. |
dcoban | 0:1a61c61d0845 | 121 | Examples: "HTTP/1.0 200 OK\r\n" -or- "HTTP/1.0 404 File no found\r\n" |
dcoban | 0:1a61c61d0845 | 122 | |
dcoban | 0:1a61c61d0845 | 123 | @param pStatusLineLength points to the length to be filled in by the |
dcoban | 0:1a61c61d0845 | 124 | application indicating the length, in character, of the returned |
dcoban | 0:1a61c61d0845 | 125 | status line. It should not include the NULL terminator. It |
dcoban | 0:1a61c61d0845 | 126 | must have a non-zero value. |
dcoban | 0:1a61c61d0845 | 127 | |
dcoban | 0:1a61c61d0845 | 128 | @returns a pointer to the status line to be returned to the HTTP client |
dcoban | 0:1a61c61d0845 | 129 | for this request. It should start with the "HTTP/1.0" string |
dcoban | 0:1a61c61d0845 | 130 | and end with a newline designator of "\r\n". The return value |
dcoban | 0:1a61c61d0845 | 131 | can't be NULL. |
dcoban | 0:1a61c61d0845 | 132 | */ |
dcoban | 0:1a61c61d0845 | 133 | virtual const char* GetStatusLine(size_t* pStatusLineLength) = 0; |
dcoban | 0:1a61c61d0845 | 134 | |
dcoban | 0:1a61c61d0845 | 135 | /** Called by the CHTTPServer object to get all of the response headers |
dcoban | 0:1a61c61d0845 | 136 | that the application would like to be returned to the HTTP client. The |
dcoban | 0:1a61c61d0845 | 137 | CHTTPServer will append this header string to the |
dcoban | 0:1a61c61d0845 | 138 | "Server: ServerName\r\n" header that it always sends. |
dcoban | 0:1a61c61d0845 | 139 | |
dcoban | 0:1a61c61d0845 | 140 | Example: "Content-Length: 100\r\n" |
dcoban | 0:1a61c61d0845 | 141 | |
dcoban | 0:1a61c61d0845 | 142 | @param pResponseHeaderLength points to the length to be filled in by |
dcoban | 0:1a61c61d0845 | 143 | the application indicating the length, in character, of the |
dcoban | 0:1a61c61d0845 | 144 | returned header string. It should not include the NULL |
dcoban | 0:1a61c61d0845 | 145 | terminator and can be 0 if the returned value is NULL. |
dcoban | 0:1a61c61d0845 | 146 | |
dcoban | 0:1a61c61d0845 | 147 | @returns a pointer to the extra headers to be returned to the HTTP |
dcoban | 0:1a61c61d0845 | 148 | client for this request. It can be NULL if the application |
dcoban | 0:1a61c61d0845 | 149 | has no special headers to be returned. |
dcoban | 0:1a61c61d0845 | 150 | */ |
dcoban | 0:1a61c61d0845 | 151 | virtual const char* GetResponseHeaders(size_t* pResponseHeaderLength) = 0; |
dcoban | 0:1a61c61d0845 | 152 | |
dcoban | 0:1a61c61d0845 | 153 | /** Called by CHTTPServer to let application know that there will be |
dcoban | 0:1a61c61d0845 | 154 | subsequent ReadResponseContentBlock() calls to obtain the content data |
dcoban | 0:1a61c61d0845 | 155 | from the application to be sent back to the HTTP client in the |
dcoban | 0:1a61c61d0845 | 156 | response. |
dcoban | 0:1a61c61d0845 | 157 | |
dcoban | 0:1a61c61d0845 | 158 | @returns 0 if the application has no content to be sent back in the |
dcoban | 0:1a61c61d0845 | 159 | HTTP response. |
dcoban | 0:1a61c61d0845 | 160 | */ |
dcoban | 0:1a61c61d0845 | 161 | virtual int BeginResponseContent() = 0; |
dcoban | 0:1a61c61d0845 | 162 | |
dcoban | 0:1a61c61d0845 | 163 | /** Called by CHTTPServer to get the next block of response data to be |
dcoban | 0:1a61c61d0845 | 164 | sent back to the HTTP client. |
dcoban | 0:1a61c61d0845 | 165 | |
dcoban | 0:1a61c61d0845 | 166 | @param pBuffer is a pointer to the buffer to be filled in by the |
dcoban | 0:1a61c61d0845 | 167 | application with data to be sent back to the HTTP client in the |
dcoban | 0:1a61c61d0845 | 168 | response. |
dcoban | 0:1a61c61d0845 | 169 | @param BytesToRead indicates the number of bytes that should be placed |
dcoban | 0:1a61c61d0845 | 170 | into pBuffer by the application. The application should only |
dcoban | 0:1a61c61d0845 | 171 | place fewer than this requested amount on the last block of |
dcoban | 0:1a61c61d0845 | 172 | data to be sent back to the HTTP client. |
dcoban | 0:1a61c61d0845 | 173 | |
dcoban | 0:1a61c61d0845 | 174 | @returns The number of bytes that were actually placed in pBuffer for |
dcoban | 0:1a61c61d0845 | 175 | this call. It can only be smaller than BytesToRead on the |
dcoban | 0:1a61c61d0845 | 176 | last block to be sent since the server uses such truncated |
dcoban | 0:1a61c61d0845 | 177 | reads to know when it has sent the last block of response |
dcoban | 0:1a61c61d0845 | 178 | data. |
dcoban | 0:1a61c61d0845 | 179 | */ |
dcoban | 0:1a61c61d0845 | 180 | virtual size_t ReadResponseContentBlock(char* pBuffer, |
dcoban | 0:1a61c61d0845 | 181 | size_t BytesToRead) = 0; |
dcoban | 0:1a61c61d0845 | 182 | |
dcoban | 0:1a61c61d0845 | 183 | |
dcoban | 0:1a61c61d0845 | 184 | /** Called by CHTTPServer to let the application know that it has finished |
dcoban | 0:1a61c61d0845 | 185 | sending all of the response data to the HTTP client. The application |
dcoban | 0:1a61c61d0845 | 186 | can use such a call to close any files that it has open for satsifying |
dcoban | 0:1a61c61d0845 | 187 | this request. |
dcoban | 0:1a61c61d0845 | 188 | */ |
dcoban | 0:1a61c61d0845 | 189 | virtual void EndResponseContent() = 0; |
dcoban | 0:1a61c61d0845 | 190 | |
dcoban | 0:1a61c61d0845 | 191 | /** Called by the CHTTPServer object when it is completely done processing |
dcoban | 0:1a61c61d0845 | 192 | this HTTP response request so that the application can free up any |
dcoban | 0:1a61c61d0845 | 193 | resources it has associated with this HTTP client request, including the |
dcoban | 0:1a61c61d0845 | 194 | IHTTPRequestHandlerContext derived object servicing these callbacks. |
dcoban | 0:1a61c61d0845 | 195 | */ |
dcoban | 0:1a61c61d0845 | 196 | virtual void Release() = 0; |
dcoban | 0:1a61c61d0845 | 197 | }; |
dcoban | 0:1a61c61d0845 | 198 | |
dcoban | 0:1a61c61d0845 | 199 | |
dcoban | 0:1a61c61d0845 | 200 | /** The application can provide an IRequestHandler interface implementation to |
dcoban | 0:1a61c61d0845 | 201 | * the HTTP server object. The methods on such attached interface objects will |
dcoban | 0:1a61c61d0845 | 202 | * be called by the server before it attempts default handling of HTTP |
dcoban | 0:1a61c61d0845 | 203 | * requests. This gives the application an opportunity to provide overrides |
dcoban | 0:1a61c61d0845 | 204 | * for handling POST requests or GET requests in a certain part of the URI |
dcoban | 0:1a61c61d0845 | 205 | * namespace. |
dcoban | 0:1a61c61d0845 | 206 | */ |
dcoban | 0:1a61c61d0845 | 207 | class IHTTPRequestHandler |
dcoban | 0:1a61c61d0845 | 208 | { |
dcoban | 0:1a61c61d0845 | 209 | public: |
dcoban | 0:1a61c61d0845 | 210 | /** Called by the server for each GET request. If the application would |
dcoban | 0:1a61c61d0845 | 211 | like to handle the GET request rather than using the default server |
dcoban | 0:1a61c61d0845 | 212 | code then the application should return a valid |
dcoban | 0:1a61c61d0845 | 213 | IHTTPRequestHandlerContext object pointer on which the server will call |
dcoban | 0:1a61c61d0845 | 214 | methods. These method calls will inform the application of which |
dcoban | 0:1a61c61d0845 | 215 | headers were sent in the request and then obtain the response data to |
dcoban | 0:1a61c61d0845 | 216 | be sent back to the HTTP client. Returning a NULL pointer indicates |
dcoban | 0:1a61c61d0845 | 217 | that the server should search in its pRootPathname directory for the |
dcoban | 0:1a61c61d0845 | 218 | requested file. |
dcoban | 0:1a61c61d0845 | 219 | |
dcoban | 0:1a61c61d0845 | 220 | @param pURI is a pointer to the URI being requested by the HTTP |
dcoban | 0:1a61c61d0845 | 221 | client. This is a short lived object that will only be |
dcoban | 0:1a61c61d0845 | 222 | valid during this call. |
dcoban | 0:1a61c61d0845 | 223 | |
dcoban | 0:1a61c61d0845 | 224 | @returns NULL if the CHTTPServer object should process the GET |
dcoban | 0:1a61c61d0845 | 225 | request on its own. Otherwise return an |
dcoban | 0:1a61c61d0845 | 226 | IHTTPRequestHandlerContext object pointer so that the |
dcoban | 0:1a61c61d0845 | 227 | application can be notified of additional request |
dcoban | 0:1a61c61d0845 | 228 | information and provide the response data. |
dcoban | 0:1a61c61d0845 | 229 | */ |
dcoban | 0:1a61c61d0845 | 230 | virtual IHTTPRequestHandlerContext* HandleGetRequest(const char* pURI) = 0; |
dcoban | 0:1a61c61d0845 | 231 | |
dcoban | 0:1a61c61d0845 | 232 | /** Called by the server for each HEAD request. If the application would |
dcoban | 0:1a61c61d0845 | 233 | like to handle the HEAD request rather than using the default server |
dcoban | 0:1a61c61d0845 | 234 | code then the application should return a valid |
dcoban | 0:1a61c61d0845 | 235 | IHTTPRequestHandlerContext object pointer on which the server will call |
dcoban | 0:1a61c61d0845 | 236 | methods. These method calls will inform the application of which |
dcoban | 0:1a61c61d0845 | 237 | headers were sent in the request and then obtain the response data to |
dcoban | 0:1a61c61d0845 | 238 | be sent back to the HTTP client. Returning a NULL pointer indicates |
dcoban | 0:1a61c61d0845 | 239 | that the server should search in its pRootPathname directory for the |
dcoban | 0:1a61c61d0845 | 240 | requested file. |
dcoban | 0:1a61c61d0845 | 241 | |
dcoban | 0:1a61c61d0845 | 242 | @param pURI is a pointer to the URI being requested by the HTTP |
dcoban | 0:1a61c61d0845 | 243 | client. This is a short lived object that will only be |
dcoban | 0:1a61c61d0845 | 244 | valid during this call. |
dcoban | 0:1a61c61d0845 | 245 | |
dcoban | 0:1a61c61d0845 | 246 | @returns NULL if the CHTTPServer object should process the HEAD |
dcoban | 0:1a61c61d0845 | 247 | request on its own. Otherwise return an |
dcoban | 0:1a61c61d0845 | 248 | IHTTPRequestHandlerContext object pointer so that the |
dcoban | 0:1a61c61d0845 | 249 | application can be notified of additional request |
dcoban | 0:1a61c61d0845 | 250 | information. |
dcoban | 0:1a61c61d0845 | 251 | */ |
dcoban | 0:1a61c61d0845 | 252 | virtual IHTTPRequestHandlerContext* HandleHeadRequest(const char* pURI) = 0; |
dcoban | 0:1a61c61d0845 | 253 | |
dcoban | 0:1a61c61d0845 | 254 | /** Called by the server for each POST request. If the application would |
dcoban | 0:1a61c61d0845 | 255 | like to handle the POST request then it should return a valid |
dcoban | 0:1a61c61d0845 | 256 | IHTTPRequestHandlerContext object pointer on which the server will call |
dcoban | 0:1a61c61d0845 | 257 | methods. These method calls will inform the application of which |
dcoban | 0:1a61c61d0845 | 258 | headers were sent in the request and then obtain the response data to |
dcoban | 0:1a61c61d0845 | 259 | be sent back to the HTTP client. Returning a NULL pointer indicates |
dcoban | 0:1a61c61d0845 | 260 | that the server should fail this POST request with a not implemented |
dcoban | 0:1a61c61d0845 | 261 | error. |
dcoban | 0:1a61c61d0845 | 262 | |
dcoban | 0:1a61c61d0845 | 263 | @param pURI is a pointer to the URI being requested by the HTTP |
dcoban | 0:1a61c61d0845 | 264 | client. This is a short lived object that will only be |
dcoban | 0:1a61c61d0845 | 265 | valid during this call. |
dcoban | 0:1a61c61d0845 | 266 | |
dcoban | 0:1a61c61d0845 | 267 | @returns NULL if the CHTTPServer object should fail the POST |
dcoban | 0:1a61c61d0845 | 268 | request. Otherwise return an IHTTPRequestHandlerContext |
dcoban | 0:1a61c61d0845 | 269 | object pointer so that the application can be notified of |
dcoban | 0:1a61c61d0845 | 270 | additional request information and provide the response data. |
dcoban | 0:1a61c61d0845 | 271 | */ |
dcoban | 0:1a61c61d0845 | 272 | virtual IHTTPRequestHandlerContext* HandlePostRequest(const char* pURI) = 0; |
dcoban | 0:1a61c61d0845 | 273 | |
dcoban | 0:1a61c61d0845 | 274 | /** Called by the server for each unrecognized request. If the application |
dcoban | 0:1a61c61d0845 | 275 | would to handle such request then it should return a valid |
dcoban | 0:1a61c61d0845 | 276 | IHTTPRequestHandlerContext object pointer on which the server will call |
dcoban | 0:1a61c61d0845 | 277 | methods. These method calls will inform the application of which |
dcoban | 0:1a61c61d0845 | 278 | headers were sent in the request and then obtain the response data to |
dcoban | 0:1a61c61d0845 | 279 | be sent back to the HTTP client. Returning a NULL pointer indicates |
dcoban | 0:1a61c61d0845 | 280 | that the server should fail this bad request with an appropriate error. |
dcoban | 0:1a61c61d0845 | 281 | |
dcoban | 0:1a61c61d0845 | 282 | @param pRequest is a pointer to the request being made by the HTTP |
dcoban | 0:1a61c61d0845 | 283 | client. This is a short lived object that will only be |
dcoban | 0:1a61c61d0845 | 284 | valid during this call. |
dcoban | 0:1a61c61d0845 | 285 | |
dcoban | 0:1a61c61d0845 | 286 | @returns NULL if the CHTTPServer object should fail this |
dcoban | 0:1a61c61d0845 | 287 | request. Otherwise return an IHTTPRequestHandlerContext |
dcoban | 0:1a61c61d0845 | 288 | object pointer so that the application can be notified of |
dcoban | 0:1a61c61d0845 | 289 | additional request information and provide the response data. |
dcoban | 0:1a61c61d0845 | 290 | */ |
dcoban | 0:1a61c61d0845 | 291 | virtual IHTTPRequestHandlerContext* HandleBadRequest(const char* pRequest) = 0; |
dcoban | 0:1a61c61d0845 | 292 | }; |
dcoban | 0:1a61c61d0845 | 293 | |
dcoban | 0:1a61c61d0845 | 294 | |
dcoban | 0:1a61c61d0845 | 295 | // Forward declaration of class used to hold context for each HTTP client |
dcoban | 0:1a61c61d0845 | 296 | // connection. |
dcoban | 0:1a61c61d0845 | 297 | class CHTTPContext; |
dcoban | 0:1a61c61d0845 | 298 | |
dcoban | 0:1a61c61d0845 | 299 | |
dcoban | 0:1a61c61d0845 | 300 | /** HTTP server class. |
dcoban | 0:1a61c61d0845 | 301 | */ |
dcoban | 0:1a61c61d0845 | 302 | class CHTTPServer |
dcoban | 0:1a61c61d0845 | 303 | { |
dcoban | 0:1a61c61d0845 | 304 | public: |
dcoban | 0:1a61c61d0845 | 305 | /** Constructor */ |
dcoban | 0:1a61c61d0845 | 306 | CHTTPServer(); |
dcoban | 0:1a61c61d0845 | 307 | |
dcoban | 0:1a61c61d0845 | 308 | /** Attach IRequestHandler to the HTTP server to allow application to add |
dcoban | 0:1a61c61d0845 | 309 | custom GET/HEAD/POST handling. |
dcoban | 0:1a61c61d0845 | 310 | |
dcoban | 0:1a61c61d0845 | 311 | @param pHandler is a pointer to the application specific request handler object |
dcoban | 0:1a61c61d0845 | 312 | to be used by the HTTP server when it receives GET/POST requests. |
dcoban | 0:1a61c61d0845 | 313 | |
dcoban | 0:1a61c61d0845 | 314 | @returns 0 on successful attachment and a positive value otherwise. |
dcoban | 0:1a61c61d0845 | 315 | */ |
dcoban | 0:1a61c61d0845 | 316 | int AttachRequestHandler(IHTTPRequestHandler* pHandler); |
dcoban | 0:1a61c61d0845 | 317 | |
dcoban | 0:1a61c61d0845 | 318 | /** Initializes the HTTP server object by binding it to to the specified |
dcoban | 0:1a61c61d0845 | 319 | port number. |
dcoban | 0:1a61c61d0845 | 320 | |
dcoban | 0:1a61c61d0845 | 321 | @param pRootPathame is the pathname of the default root directory from which the |
dcoban | 0:1a61c61d0845 | 322 | HTTP GET requests are satisfied. |
dcoban | 0:1a61c61d0845 | 323 | @param pServerName is the name of the server to be returned to the HTTP client |
dcoban | 0:1a61c61d0845 | 324 | in the Server header. |
dcoban | 0:1a61c61d0845 | 325 | @param BindPort is the TCP/IP port to which the HTTP server should listen for |
dcoban | 0:1a61c61d0845 | 326 | incoming requests. The default port for HTTP would be 80. |
dcoban | 0:1a61c61d0845 | 327 | |
dcoban | 0:1a61c61d0845 | 328 | @returns 0 on success and a positive value otherwise. |
dcoban | 0:1a61c61d0845 | 329 | */ |
dcoban | 0:1a61c61d0845 | 330 | int Bind(const char* pRootPathname, |
dcoban | 0:1a61c61d0845 | 331 | const char* pServerName = "lwIP_HTTPServer/1.0", |
dcoban | 0:1a61c61d0845 | 332 | unsigned short BindPort = 80); |
dcoban | 0:1a61c61d0845 | 333 | |
dcoban | 0:1a61c61d0845 | 334 | protected: |
dcoban | 0:1a61c61d0845 | 335 | // Static Method Prototypes for lwIP Callbacks. |
dcoban | 0:1a61c61d0845 | 336 | static err_t HTTPAccept(void* pvArg, tcp_pcb* pNewPCB, err_t err); |
dcoban | 0:1a61c61d0845 | 337 | |
dcoban | 0:1a61c61d0845 | 338 | // Methods called from CHTTPContext. |
dcoban | 0:1a61c61d0845 | 339 | friend class CHTTPContext; |
dcoban | 0:1a61c61d0845 | 340 | void FreeBuffer(SBuffer* pBuffer); |
dcoban | 0:1a61c61d0845 | 341 | void RemoveWaitingContext(CHTTPContext* pHTTPContext); |
dcoban | 0:1a61c61d0845 | 342 | SBuffer* AllocateBuffer(CHTTPContext* pHTTPContext); |
dcoban | 0:1a61c61d0845 | 343 | |
dcoban | 0:1a61c61d0845 | 344 | // Listening port for HTTP Server. |
dcoban | 0:1a61c61d0845 | 345 | tcp_pcb* m_pHTTPListenPCB; |
dcoban | 0:1a61c61d0845 | 346 | // Head and tail pointers for queue of client contexts that are waiting to |
dcoban | 0:1a61c61d0845 | 347 | // be allocated a SBuffer from the BufferPool. |
dcoban | 0:1a61c61d0845 | 348 | CHTTPContext* m_pContextWaitingHead; |
dcoban | 0:1a61c61d0845 | 349 | CHTTPContext* m_pContextWaitingTail; |
dcoban | 0:1a61c61d0845 | 350 | // The default root directory from which the HTTP GET requests are satisfied. |
dcoban | 0:1a61c61d0845 | 351 | const char* m_pRootPathname; |
dcoban | 0:1a61c61d0845 | 352 | // Pointer to interface from application allowing it to handle HTTP requests. |
dcoban | 0:1a61c61d0845 | 353 | IHTTPRequestHandler* m_pRequestHandler; |
dcoban | 0:1a61c61d0845 | 354 | // Server header returned to HTTP clients. |
dcoban | 0:1a61c61d0845 | 355 | size_t m_ServerHeaderLength; |
dcoban | 0:1a61c61d0845 | 356 | char m_ServerHeader[64]; |
dcoban | 0:1a61c61d0845 | 357 | // Pool of buffers to use for transitioning data from the filesystem to |
dcoban | 0:1a61c61d0845 | 358 | // the network. |
dcoban | 0:1a61c61d0845 | 359 | SBuffer m_BufferPool[HTTP_BUFFER_POOL]; |
dcoban | 0:1a61c61d0845 | 360 | }; |
dcoban | 0:1a61c61d0845 | 361 | |
dcoban | 0:1a61c61d0845 | 362 | |
dcoban | 0:1a61c61d0845 | 363 | #endif /* HTTPSERVER_H_ */ |