server-name: add_server_header add LWSAHH_FLAG_NO_SERVER_NAME
authorAndy Green <andy@warmcat.com>
Tue, 7 Mar 2017 23:51:47 +0000 (07:51 +0800)
committerAndy Green <andy@warmcat.com>
Tue, 7 Mar 2017 23:51:47 +0000 (07:51 +0800)
Also clean up usage of status code defines in lws

lib/header.c
lib/libwebsockets.h
lib/server.c
plugins/generic-sessions/handlers.c
plugins/generic-sessions/protocol_lws_messageboard.c
plugins/protocol_post_demo.c
test-server/test-server-http.c

index 46bdb03..036abe0 100644 (file)
@@ -143,16 +143,17 @@ STORE_IN_ROM static const char * const err500[] = {
 };
 
 int
-lws_add_http_header_status(struct lws *wsi, unsigned int code,
+lws_add_http_header_status(struct lws *wsi, unsigned int _code,
                           unsigned char **p, unsigned char *end)
 {
-       const struct lws_protocol_vhost_options *headers;
-       unsigned char code_and_desc[60];
-       const char *description = "", *p1;
-       int n;
        STORE_IN_ROM static const char * const hver[] = {
                "HTTP/1.0", "HTTP/1.1", "HTTP/2"
        };
+       const struct lws_protocol_vhost_options *headers;
+       unsigned int code = _code & LWSAHH_CODE_MASK;
+       const char *description = "", *p1;
+       unsigned char code_and_desc[60];
+       int n;
 
 #ifdef LWS_WITH_ACCESS_LOG
        wsi->access_log.response = code;
@@ -181,11 +182,9 @@ lws_add_http_header_status(struct lws *wsi, unsigned int code,
        else
                p1 = hver[0];
 
-       n = sprintf((char *)code_and_desc, "%s %u %s",
-                   p1, code, description);
+       n = sprintf((char *)code_and_desc, "%s %u %s", p1, code, description);
 
-       if (lws_add_http_header_by_name(wsi, NULL, code_and_desc,
-                                          n, p, end))
+       if (lws_add_http_header_by_name(wsi, NULL, code_and_desc, n, p, end))
                return 1;
 
        headers = wsi->vhost->headers;
@@ -199,13 +198,11 @@ lws_add_http_header_status(struct lws *wsi, unsigned int code,
                headers = headers->next;
        }
 
-       if (wsi->context->server_string)
-               if (lws_add_http_header_by_token(wsi,
-                                        WSI_TOKEN_HTTP_SERVER,
-                                        (unsigned char *)
-                                                wsi->context->server_string,
-                                        wsi->context->server_string_len,
-                                        p, end))
+       if (wsi->context->server_string &&
+           !(_code & LWSAHH_FLAG_NO_SERVER_NAME))
+               if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_SERVER,
+                               (unsigned char *)wsi->context->server_string,
+                               wsi->context->server_string_len, p, end))
                        return 1;
 
        if (wsi->vhost->options & LWS_SERVER_OPTION_STS)
index d76a558..ffdcc84 100644 (file)
@@ -2589,6 +2589,7 @@ enum http_status {
        HTTP_STATUS_MOVED_PERMANENTLY                           = 301,
        HTTP_STATUS_FOUND                                       = 302,
        HTTP_STATUS_SEE_OTHER                                   = 303,
+       HTTP_STATUS_NOT_MODIFIED                                = 304,
 
        HTTP_STATUS_BAD_REQUEST                                 = 400,
        HTTP_STATUS_UNAUTHORIZED,
@@ -2909,6 +2910,10 @@ lws_get_urlarg_by_name(struct lws *wsi, const char *name, char *buf, int len);
  * and fail with nonzero return.
  */
 ///@{
+
+#define LWSAHH_CODE_MASK                       ((1 << 16) - 1)
+#define LWSAHH_FLAG_NO_SERVER_NAME             (1 << 30)
+
 /**
  * lws_add_http_header_status() - add the HTTP response status code
  *
@@ -2917,7 +2922,11 @@ lws_get_urlarg_by_name(struct lws *wsi, const char *name, char *buf, int len);
  * \param p: pointer to current position in buffer pointer
  * \param end: pointer to end of buffer
  *
- * Adds the initial response code, so should be called first
+ * Adds the initial response code, so should be called first.
+ *
+ * Code may additionally take OR'd flags:
+ *
+ *    LWSAHH_FLAG_NO_SERVER_NAME:  don't apply server name header this time
  */
 LWS_VISIBLE LWS_EXTERN int LWS_WARN_UNUSED_RESULT
 lws_add_http_header_status(struct lws *wsi,
index 94eb0d0..26884d8 100644 (file)
@@ -445,7 +445,8 @@ lws_http_serve(struct lws *wsi, char *uri, const char *origin,
                                   uri, origin);
 
                        /* we don't need to send the payload */
-                       if (lws_add_http_header_status(wsi, 304, &p, end))
+                       if (lws_add_http_header_status(wsi,
+                                       HTTP_STATUS_NOT_MODIFIED, &p, end))
                                return -1;
 
                        if (lws_add_http_header_by_token(wsi,
@@ -1066,7 +1067,7 @@ lws_http_action(struct lws *wsi)
                p = buffer + LWS_PRE;
                end = p + sizeof(buffer) - LWS_PRE;
 
-               if (lws_add_http_header_status(wsi, 200, &p, end))
+               if (lws_add_http_header_status(wsi, HTTP_STATUS_OK, &p, end))
                        return 1;
                if (lws_add_http_header_by_token(wsi, WSI_TOKEN_CONNECTION,
                                (unsigned char *)"close", 5, &p, end))
index f450a19..90276e5 100644 (file)
@@ -243,7 +243,7 @@ reply:
        start = p;
        end = p + sizeof(buffer) - LWS_PRE;
 
-       if (lws_add_http_header_status(wsi, 200, &p, end))
+       if (lws_add_http_header_status(wsi, HTTP_STATUS_OK, &p, end))
                return -1;
        if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
                                         (unsigned char *)"text/plain", 10,
index 4b2275c..8cb2d77 100644 (file)
@@ -319,7 +319,7 @@ callback_messageboard(struct lws *wsi, enum lws_callback_reasons reason,
                start = p;
                end = p + sizeof(buffer) - LWS_PRE;
 
-               if (lws_add_http_header_status(wsi, 200, &p, end))
+               if (lws_add_http_header_status(wsi, HTTP_STATUS_OK, &p, end))
                        return -1;
                if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
                                (unsigned char *)"text/plain", 10, &p, end))
index 18b6b60..bd9bef6 100644 (file)
@@ -165,7 +165,7 @@ callback_post_demo(struct lws *wsi, enum lws_callback_reasons reason,
                start = p;
                end = p + n - LWS_PRE - 1;
 
-               if (lws_add_http_header_status(wsi, 200, &p, end))
+               if (lws_add_http_header_status(wsi, HTTP_STATUS_OK, &p, end))
                        goto bail;
 
                if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
index 3b3029d..81445ca 100644 (file)
@@ -311,7 +311,7 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
                         * depending on what connection it happens to be working
                         * on
                         */
-                       if (lws_add_http_header_status(wsi, 200, &p, end))
+                       if (lws_add_http_header_status(wsi, HTTP_STATUS_OK, &p, end))
                                return 1;
                        if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_SERVER,
                                        (unsigned char *)"libwebsockets",
@@ -473,7 +473,7 @@ int callback_http(struct lws *wsi, enum lws_callback_reasons reason, void *user,
                start = p;
                end = p + sizeof(buffer) - LWS_PRE;
 
-               if (lws_add_http_header_status(wsi, 200, &p, end))
+               if (lws_add_http_header_status(wsi, HTTP_STATUS_OK, &p, end))
                        return 1;
 
                if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_CONTENT_TYPE,
@@ -615,7 +615,7 @@ bail:
                lwsl_err("LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP\n");
                p = buffer + LWS_PRE;
                end = p + sizeof(buffer) - LWS_PRE;
-               if (lws_add_http_header_status(lws_get_parent(wsi), 200, &p, end))
+               if (lws_add_http_header_status(lws_get_parent(wsi), HTTP_STATUS_OK, &p, end))
                        return 1;
                if (lws_add_http_header_by_token(lws_get_parent(wsi),
                                WSI_TOKEN_HTTP_SERVER,