Subject: lws_stats: fix compile error on VS2013
[platform/upstream/libwebsockets.git] / lib / hpack.c
index 88aa291..a205cbc 100644 (file)
@@ -199,11 +199,15 @@ static int lws_frag_start(struct lws *wsi, int hdr_token_idx)
 {
        struct allocated_headers * ah = wsi->u.http2.http.ah;
 
-       if (!hdr_token_idx)
+       if (!hdr_token_idx) {
+               lwsl_err("%s: zero hdr_token_idx\n", __func__);
                return 1;
+       }
 
-       if (ah->nfrag >= ARRAY_SIZE(ah->frag_index))
+       if (ah->nfrag >= ARRAY_SIZE(ah->frag_index)) {
+               lwsl_err("%s: frag index %d too big\n", __func__, ah->nfrag);
                return 1;
+       }
 
        ah->frags[ah->nfrag].offset = ah->pos;
        ah->frags[ah->nfrag].len = 0;
@@ -221,7 +225,7 @@ static int lws_frag_append(struct lws *wsi, unsigned char c)
        ah->data[ah->pos++] = c;
        ah->frags[ah->nfrag].len++;
 
-       return ah->pos >= sizeof(ah->data);
+       return ah->pos >= wsi->context->max_http_header_data;
 }
 
 static int lws_frag_end(struct lws *wsi)
@@ -349,13 +353,14 @@ static int lws_write_indexed_hdr(struct lws *wsi, int idx)
        return 0;
 }
 
-int lws_hpack_interpret(struct lws_context *context,
-                       struct lws *wsi, unsigned char c)
+int lws_hpack_interpret(struct lws *wsi, unsigned char c)
 {
        unsigned int prev;
        unsigned char c1;
        int n;
 
+       lwsl_debug("   state %d\n", wsi->u.http2.hpack);
+
        switch (wsi->u.http2.hpack) {
        case HPKS_OPT_PADDING:
                wsi->u.http2.padding = c;
@@ -389,6 +394,7 @@ int lws_hpack_interpret(struct lws_context *context,
                if (c & 0x80) { /* indexed header field only */
                        /* just a possibly-extended integer */
                        wsi->u.http2.hpack_type = HPKT_INDEXED_HDR_7;
+                       lwsl_debug("HKPS_TYPE setting header_index %d\n", c & 0x7f);
                        wsi->u.http2.header_index = c & 0x7f;
                        if ((c & 0x7f) == 0x7f) {
                                wsi->u.http2.hpack_len = c & 0x7f;
@@ -396,6 +402,7 @@ int lws_hpack_interpret(struct lws_context *context,
                                wsi->u.http2.hpack = HPKS_IDX_EXT;
                                break;
                        }
+                       lwsl_debug("HKPS_TYPE: %d\n", c & 0x7f);
                        if (lws_write_indexed_hdr(wsi, c & 0x7f))
                                return 1;
                        /* stay at same state */
@@ -407,6 +414,7 @@ int lws_hpack_interpret(struct lws_context *context,
                         * H + possibly-extended value length
                         * literal value
                         */
+                       lwsl_debug("HKPS_TYPE 2 setting header_index %d\n", 0);
                        wsi->u.http2.header_index = 0;
                        if (c == 0x40) { /* literal name */
                                wsi->u.http2.hpack_type = HPKT_LITERAL_HDR_VALUE_INCR;
@@ -422,6 +430,7 @@ int lws_hpack_interpret(struct lws_context *context,
                                wsi->u.http2.hpack = HPKS_IDX_EXT;
                                break;
                        }
+                       lwsl_debug("HKPS_TYPE 3 setting header_index %d\n", c & 0x3f);
                        wsi->u.http2.header_index = c & 0x3f;
                        wsi->u.http2.value = 1;
                        wsi->u.http2.hpack = HPKS_HLEN;
@@ -440,6 +449,7 @@ int lws_hpack_interpret(struct lws_context *context,
                                wsi->u.http2.value = 0;
                                break;
                        }
+                       //lwsl_debug("indexed\n");
                        /* indexed name */
                        wsi->u.http2.hpack_type = HPKT_INDEXED_HDR_4_VALUE;
                        wsi->u.http2.header_index = 0;
@@ -449,6 +459,7 @@ int lws_hpack_interpret(struct lws_context *context,
                                wsi->u.http2.hpack = HPKS_IDX_EXT;
                                break;
                        }
+                       //lwsl_err("HKPS_TYPE 5 setting header_index %d\n", c & 0xf);
                        wsi->u.http2.header_index = c & 0xf;
                        wsi->u.http2.value = 1;
                        wsi->u.http2.hpack = HPKS_HLEN;
@@ -476,11 +487,14 @@ int lws_hpack_interpret(struct lws_context *context,
                if (!(c & 0x80)) {
                        switch (wsi->u.http2.hpack_type) {
                        case HPKT_INDEXED_HDR_7:
+                               //lwsl_err("HKPS_IDX_EXT hdr idx %d\n", wsi->u.http2.hpack_len);
                                if (lws_write_indexed_hdr(wsi, wsi->u.http2.hpack_len))
                                        return 1;
                                wsi->u.http2.hpack = HPKS_TYPE;
                                break;
                        default:
+                               // lwsl_err("HKPS_IDX_EXT setting header_index %d\n",
+                               //              wsi->u.http2.hpack_len);
                                wsi->u.http2.header_index = wsi->u.http2.hpack_len;
                                wsi->u.http2.value = 1;
                                wsi->u.http2.hpack = HPKS_HLEN;
@@ -496,10 +510,13 @@ int lws_hpack_interpret(struct lws_context *context,
                if (wsi->u.http2.hpack_len < 0x7f) {
 pre_data:
                        if (wsi->u.http2.value) {
+                               if (wsi->u.http2.header_index)
                                if (lws_frag_start(wsi, lws_token_from_index(wsi,
                                                   wsi->u.http2.header_index,
-                                                  NULL, NULL)))
+                                                  NULL, NULL))) {
+                               //      lwsl_notice("%s: hlen failed\n", __func__);
                                        return 1;
+                               }
                        } else
                                wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
                        wsi->u.http2.hpack = HPKS_DATA;
@@ -540,8 +557,9 @@ pre_data:
                                c1 = c;
                        }
                        if (wsi->u.http2.value) { /* value */
-                               if (lws_frag_append(wsi, c1))
-                                       return 1;
+                               if (wsi->u.http2.header_index)
+                                       if (lws_frag_append(wsi, c1))
+                                               return 1;
                        } else { /* name */
                                if (lws_parse(wsi, c1))
                                        return 1;
@@ -567,7 +585,7 @@ pre_data:
                        if (wsi->u.http2.value) {
                                if (lws_frag_end(wsi))
                                        return 1;
-
+                               // lwsl_err("data\n");
                                lws_dump_header(wsi, lws_token_from_index(
                                                wsi, wsi->u.http2.header_index,
                                                NULL, NULL));
@@ -577,7 +595,7 @@ pre_data:
                                else
                                        wsi->u.http2.hpack = HPKS_TYPE;
                        } else { /* name */
-                               if (wsi->u.hdr.parser_state < WSI_TOKEN_COUNT)
+                               //if (wsi->u.hdr.parser_state < WSI_TOKEN_COUNT)
 
                                wsi->u.http2.value = 1;
                                wsi->u.http2.hpack = HPKS_HLEN;
@@ -619,7 +637,7 @@ static int lws_http2_num(int starting_bits, unsigned long num,
        return 0;
 }
 
-int lws_add_http2_header_by_name(struct lws_context *context, struct lws *wsi,
+int lws_add_http2_header_by_name(struct lws *wsi,
                                 const unsigned char *name,
                                 const unsigned char *value, int length,
                                 unsigned char **p, unsigned char *end)
@@ -654,8 +672,7 @@ int lws_add_http2_header_by_name(struct lws_context *context, struct lws *wsi,
        return 0;
 }
 
-int lws_add_http2_header_by_token(struct lws_context *context, struct lws *wsi,
-                                 enum lws_token_indexes token,
+int lws_add_http2_header_by_token(struct lws *wsi, enum lws_token_indexes token,
                                  const unsigned char *value, int length,
                                  unsigned char **p, unsigned char *end)
 {
@@ -665,11 +682,10 @@ int lws_add_http2_header_by_token(struct lws_context *context, struct lws *wsi,
        if (!name)
                return 1;
 
-       return lws_add_http2_header_by_name(context, wsi, name, value,
-                                           length, p, end);
+       return lws_add_http2_header_by_name(wsi, name, value, length, p, end);
 }
 
-int lws_add_http2_header_status(struct lws_context *context, struct lws *wsi,
+int lws_add_http2_header_status(struct lws *wsi,
                                unsigned int code, unsigned char **p,
                                unsigned char *end)
 {
@@ -679,9 +695,9 @@ int lws_add_http2_header_status(struct lws_context *context, struct lws *wsi,
        wsi->u.http2.send_END_STREAM = !!(code >= 400);
 
        n = sprintf((char *)status, "%u", code);
-       if (lws_add_http2_header_by_token(context, wsi,
-                                         WSI_TOKEN_HTTP_COLON_STATUS, status,
-                                         n, p, end))
+       if (lws_add_http2_header_by_token(wsi, WSI_TOKEN_HTTP_COLON_STATUS,
+                                         status, n, p, end))
+
                return 1;
 
        return 0;