tasklwIP - A Lightweight TCP/IP stack - Tasks: task #14568, httpd. Add new headers

 
 

You are not allowed to post comments on this tracker with your current authentication level.

task #14568: httpd. Add new headers

Submitter:  Andrey Vinogradov <andreyvinogradov>
Submitted:  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
Assigned to:  None Percent Complete:  0%
Open/Closed:  Open Planned Release:  None
Effort:  8.00

Tue 01 Aug 2017 07:16:15 PM UTC, comment #5: 


> Aditionally, right now I'm not sure what happens if these header pointers are left at NULL.


OK, this is not a bug in your patch but bad code in the current httpd: it can handle NULL headers, but not for the first header...

As to your patch: I'm not sure it's wise to add a default string for the "Expires" header. Wouldn't that mean products would stop working in 12.5 years?

Simon Goldschmidt <goldsimon>
Group administrator
Mon 31 Jul 2017 07:07:49 PM UTC, comment #4: 

OK, I understand your idea. However, the patch is not really complete as
a) it doesn't compile for various configurations and
b) you can't just assume to have the function getETagHeader() defined without having documented a prototype anywhere or without any ifdef for it.

Aditionally, right now I'm not sure what happens if these header pointers are left at NULL. It seems like we would call strlen() on them. The standard does not say that "strlen(NULL)==0".

However, this bug could alreay exist in the rest of the httpd code...

Simon Goldschmidt <goldsimon>
Group administrator
Thu 27 Jul 2017 12:08:57 PM UTC, comment #3: 

uploaded patches. If interested, my version of lwip is here:
https://github.com/Kira-sempai/lwip

Andrey Vinogradov <andreyvinogradov>
Wed 26 Jul 2017 08:18:48 PM UTC, comment #2: 

Really, I'm not sure I understand this post. Could you supply a patch clarifying your changes?

Simon Goldschmidt <goldsimon>
Group administrator
Wed 21 Jun 2017 08:06:50 PM UTC, comment #1: 

Wow, this is long, I need to find more time to read it...

Simon Goldschmidt <goldsimon>
Group administrator
Wed 21 Jun 2017 04:14:29 PM UTC, original submission:  

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.

Andrey Vinogradov <andreyvinogradov>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attached Files
file #41358:  0001-include-lwip-lwip_hooks.h-add-hooks.patch added by andreyvinogradov (854B - application/octet-stream - patch changes. getETagHeader() function should be implemented (probably by user) and return string like "ETag: \"12345\"\r\n")
file #41359:  0002-apps-httpd-add-ETag-header-support.patch added by andreyvinogradov (4KiB - application/octet-stream - patch changes. getETagHeader() function should be implemented (probably by user) and return string like "ETag: \"12345\"\r\n")

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by goldsimon (Posted a comment)
  • -email is unavailable- added by andreyvinogradov (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

     

    Follow 4 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2018-02-04 goldsimon Planned Release2.1.0 None
    2017-11-17 goldsimon Planned ReleaseNone 2.1.0
    2017-07-27 andreyvinogradov Attached File- Added 0001-include-lwip-lwip_hooks.h-add-hooks.patch, #41358
        Attached File- Added 0002-apps-httpd-add-ETag-header-support.patch, #41359

    Back to the top

    Powered by Savane 3.13-081f.
    Corresponding source code