lwip-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lwip-devel] [task #14568] httpd. Add new headers


From: Andrey Vinogradov
Subject: [lwip-devel] [task #14568] httpd. Add new headers
Date: Wed, 21 Jun 2017 12:14:30 -0400 (EDT)
User-agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36

URL:
  <http://savannah.nongnu.org/task/?14568>

                 Summary: httpd. Add new headers
                 Project: lwIP - A Lightweight TCP/IP stack
            Submitted by: andreyvinogradov
            Submitted on: Wed 21 Jun 2017 04:14:29 PM UTC
                Category: apps
         Should Start On: Wed 21 Jun 2017 12:00:00 AM UTC
   Should be Finished on: Wed 21 Jun 2017 12:00:00 AM UTC
                Priority: 1 - Later
                  Status: None
                 Privacy: Public
        Percent Complete: 0%
             Assigned to: None
             Open/Closed: Open
         Discussion Lock: Any
         Planned Release: None
                  Effort: 8.00

    _______________________________________________________

Details:

You have httpd.c file to control GET/POST requests. It works fine. I run my
web-server on stm32 microcontroller using it.

But I had to change it with my workarounds to get extra functions as follows:

- ETag and Cache-Control headers for files transmit
- Cookie: Session ID read (and probably other parameter-value pairs)

ETag header allow me to open pages faster. To get this headers work I need
following lines in code:
httpd.c:
"""
....
/* The number of individual strings that comprise the headers sent before
each
 * requested file.
 */
#define NUM_FILE_HDR_STRINGS 7
#define HDR_STRINGS_IDX_HTTP_STATUS          0 /* e.g. "HTTP/1.0 200 OK\r\n"
*/
#define HDR_STRINGS_IDX_SERVER_NAME          1 /* e.g. "Server:
"HTTPD_SERVER_AGENT"\r\n" */
#define HDR_STRINGS_IDX_CONTENT_LEN_KEPALIVE 2 /* e.g. "Content-Length:
xy\r\n" and/or "Connection: keep-alive\r\n" */
#define HDR_STRINGS_IDX_CONTENT_LEN_NR       3 /* the byte count, when
content-length is used */
#define HDR_STRINGS_IDX_ETAG                 4 /* e.g. "ETag: "2073041041""
*/
#define HDR_STRINGS_IDX_CACHE_CONTROL_MAX_AGE 5 /* e.g. "Cache-Control:
max-age=31536000\r\nExpires: Tue, 01 Jan 2030 00:00:00 GMT\r\n" */
#define HDR_STRINGS_IDX_CONTENT_TYPE         6 /* the content type (or default
answer content type including default document) */
.....


/**
 * Generate the relevant HTTP headers for the given filename and write
 * them into the supplied buffer.
 */
static void
get_http_headers(struct http_state *hs, const char *uri)
....
  /* We are dealing with a particular filename. Look for one other
      special case.  We assume that any filename with "404" in it must be
      indicative of a 404 server error whereas all other files require
      the 200 OK header. */
  if (strstr(uri, "404")) {
    hs->hdrs[HDR_STRINGS_IDX_HTTP_STATUS] =
g_psHTTPHeaderStrings[HTTP_HDR_NOT_FOUND];
  } else if (strstr(uri, "400")) {
    hs->hdrs[HDR_STRINGS_IDX_HTTP_STATUS] =
g_psHTTPHeaderStrings[HTTP_HDR_BAD_REQUEST];
  } else if (strstr(uri, "501")) {
    hs->hdrs[HDR_STRINGS_IDX_HTTP_STATUS] =
g_psHTTPHeaderStrings[HTTP_HDR_NOT_IMPL];
  } else if (strstr(uri, "304")) {
    hs->hdrs[HDR_STRINGS_IDX_HTTP_STATUS] =
g_psHTTPHeaderStrings[HTTP_HDR_NOT_MODIFIED];
    hs->hdrs[HDR_STRINGS_IDX_CACHE_CONTROL_MAX_AGE] =
g_psHTTPHeaderStrings[CACHE_CONTROL_MAX_AGE];
    hs->hdr_index = 0;
    hs->hdr_pos = 0;
    return;
  } else {
    hs->hdrs[HDR_STRINGS_IDX_HTTP_STATUS] =
g_psHTTPHeaderStrings[HTTP_HDR_OK];
  }
....

  /* Reinstate the parameter marker if there was one in the original URI. */
  if (vars) {
    *vars = '?';
  }

  if (hs->handle && hs->handle->is_custom_file){
    char * const eTagHeader = getETagHeader(hs->handle->pextension);
    if (eTagHeader != NULL) {
      hs->hdrs[HDR_STRINGS_IDX_ETAG] = eTagHeader;
      hs->hdrs[HDR_STRINGS_IDX_CACHE_CONTROL_MAX_AGE] =
g_psHTTPHeaderStrings[CACHE_CONTROL_MAX_AGE];
    }
  }
.....


"""

httpd_struct.h:
"""
...

/** A list of strings used in HTTP headers (see RFC 1945 HTTP/1.0 and
 * RFC 2616 HTTP/1.1 for header field definitions) */
static const char * const g_psHTTPHeaderStrings[] =
{
 "HTTP/1.0 200 OK\r\n",
 "HTTP/1.0 404 File not found\r\n",
 "HTTP/1.0 400 Bad Request\r\n",
 "HTTP/1.0 501 Not Implemented\r\n",
 "HTTP/1.1 200 OK\r\n",
 "HTTP/1.1 404 File not found\r\n",
 "HTTP/1.1 400 Bad Request\r\n",
 "HTTP/1.1 501 Not Implemented\r\n",
 "Content-Length: ",
 "Connection: Close\r\n",
 "Connection: keep-alive\r\n",
 "Connection: keep-alive\r\nContent-Length: ",
 "Server: "HTTPD_SERVER_AGENT"\r\n",
 "\r\n<html><body><h2>404: The requested file cannot be
found.</h2></body></html>\r\n"
#if LWIP_HTTPD_SUPPORT_11_KEEPALIVE
 ,"Connection: keep-alive\r\nContent-Length: 77\r\n\r\n<html><body><h2>404:
The requested file cannot be found.</h2></body></html>\r\n"
#endif
 ,"Cache-Control: max-age=31536000\r\nExpires: Tue, 01 Jan 2030 00:00:00
GMT\r\n",
 "HTTP/1.0 304 Not Modified\r\n",
 "HTTP/1.0 302 Found\r\n",
};
...

#if LWIP_HTTPD_SUPPORT_11_KEEPALIVE
#define DEFAULT_404_HTML_PERSISTENT 14 /* default 404 body, but including
Connection: keep-alive */
#endif
#if LWIP_HTTPD_SUPPORT_11_KEEPALIVE
#define CACHE_CONTROL_MAX_AGE   15 /* file expires in 1 year */
#define HTTP_HDR_NOT_MODIFIED   16 /* file not modified */
#define HTTP_HDR_FOUND 17 /* file not modified */
#else
#define CACHE_CONTROL_MAX_AGE   14 /* file expires in 1 year */
#define HTTP_HDR_NOT_MODIFIED   15 /* file not modified */
#define HTTP_HDR_FOUND          16 /* file not modified */
#endif
...

"""

HDR_STRINGS_IDX_CONTENT_TYPE I have to put in the end because this header MUST
be at the end, that is probably not very universal.





    _______________________________________________________

Reply to this item at:

  <http://savannah.nongnu.org/task/?14568>

_______________________________________________
  Message sent via/by Savannah
  http://savannah.nongnu.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]