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