Willy Tarreau | 0da5b3b | 2017-09-21 09:30:46 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * include/proto/h1.h |
| 3 | * This file contains HTTP/1 protocol definitions. |
| 4 | * |
| 5 | * Copyright (C) 2000-2017 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef _PROTO_H1_H |
| 23 | #define _PROTO_H1_H |
| 24 | |
| 25 | #include <common/compiler.h> |
| 26 | #include <common/config.h> |
| 27 | #include <types/h1.h> |
| 28 | |
| 29 | extern const uint8_t h1_char_classes[256]; |
| 30 | |
| 31 | #define H1_FLG_CTL 0x01 |
| 32 | #define H1_FLG_SEP 0x02 |
| 33 | #define H1_FLG_LWS 0x04 |
| 34 | #define H1_FLG_SPHT 0x08 |
| 35 | #define H1_FLG_CRLF 0x10 |
| 36 | #define H1_FLG_TOK 0x20 |
| 37 | #define H1_FLG_VER 0x40 |
| 38 | |
| 39 | #define HTTP_IS_CTL(x) (h1_char_classes[(uint8_t)(x)] & H1_FLG_CTL) |
| 40 | #define HTTP_IS_SEP(x) (h1_char_classes[(uint8_t)(x)] & H1_FLG_SEP) |
| 41 | #define HTTP_IS_LWS(x) (h1_char_classes[(uint8_t)(x)] & H1_FLG_LWS) |
| 42 | #define HTTP_IS_SPHT(x) (h1_char_classes[(uint8_t)(x)] & H1_FLG_SPHT) |
| 43 | #define HTTP_IS_CRLF(x) (h1_char_classes[(uint8_t)(x)] & H1_FLG_CRLF) |
| 44 | #define HTTP_IS_TOKEN(x) (h1_char_classes[(uint8_t)(x)] & H1_FLG_TOK) |
| 45 | #define HTTP_IS_VER_TOKEN(x) (h1_char_classes[(uint8_t)(x)] & H1_FLG_VER) |
| 46 | |
| 47 | |
| 48 | /* Macros used in the HTTP/1 parser, to check for the expected presence of |
| 49 | * certain bytes (ef: LF) or to skip to next byte and yield in case of failure. |
| 50 | */ |
| 51 | |
| 52 | |
| 53 | /* Expects to find an LF at <ptr>. If not, set <state> to <where> and jump to |
| 54 | * <bad>. |
| 55 | */ |
| 56 | #define EXPECT_LF_HERE(ptr, bad, state, where) \ |
| 57 | do { \ |
| 58 | if (unlikely(*(ptr) != '\n')) { \ |
| 59 | state = (where); \ |
| 60 | goto bad; \ |
| 61 | } \ |
| 62 | } while (0) |
| 63 | |
| 64 | /* Increments pointer <ptr>, continues to label <more> if it's still below |
| 65 | * pointer <end>, or goes to <stop> and sets <state> to <where> if the end |
| 66 | * of buffer was reached. |
| 67 | */ |
| 68 | #define EAT_AND_JUMP_OR_RETURN(ptr, end, more, stop, state, where) \ |
| 69 | do { \ |
| 70 | if (likely(++(ptr) < (end))) \ |
| 71 | goto more; \ |
| 72 | else { \ |
| 73 | state = (where); \ |
| 74 | goto stop; \ |
| 75 | } \ |
| 76 | } while (0) |
| 77 | |
| 78 | /* for debugging, reports the HTTP/1 message state name */ |
| 79 | static inline const char *h1_msg_state_str(enum h1_state msg_state) |
| 80 | { |
| 81 | switch (msg_state) { |
| 82 | case HTTP_MSG_RQBEFORE: return "MSG_RQBEFORE"; |
| 83 | case HTTP_MSG_RQBEFORE_CR: return "MSG_RQBEFORE_CR"; |
| 84 | case HTTP_MSG_RQMETH: return "MSG_RQMETH"; |
| 85 | case HTTP_MSG_RQMETH_SP: return "MSG_RQMETH_SP"; |
| 86 | case HTTP_MSG_RQURI: return "MSG_RQURI"; |
| 87 | case HTTP_MSG_RQURI_SP: return "MSG_RQURI_SP"; |
| 88 | case HTTP_MSG_RQVER: return "MSG_RQVER"; |
| 89 | case HTTP_MSG_RQLINE_END: return "MSG_RQLINE_END"; |
| 90 | case HTTP_MSG_RPBEFORE: return "MSG_RPBEFORE"; |
| 91 | case HTTP_MSG_RPBEFORE_CR: return "MSG_RPBEFORE_CR"; |
| 92 | case HTTP_MSG_RPVER: return "MSG_RPVER"; |
| 93 | case HTTP_MSG_RPVER_SP: return "MSG_RPVER_SP"; |
| 94 | case HTTP_MSG_RPCODE: return "MSG_RPCODE"; |
| 95 | case HTTP_MSG_RPCODE_SP: return "MSG_RPCODE_SP"; |
| 96 | case HTTP_MSG_RPREASON: return "MSG_RPREASON"; |
| 97 | case HTTP_MSG_RPLINE_END: return "MSG_RPLINE_END"; |
| 98 | case HTTP_MSG_HDR_FIRST: return "MSG_HDR_FIRST"; |
| 99 | case HTTP_MSG_HDR_NAME: return "MSG_HDR_NAME"; |
| 100 | case HTTP_MSG_HDR_COL: return "MSG_HDR_COL"; |
| 101 | case HTTP_MSG_HDR_L1_SP: return "MSG_HDR_L1_SP"; |
| 102 | case HTTP_MSG_HDR_L1_LF: return "MSG_HDR_L1_LF"; |
| 103 | case HTTP_MSG_HDR_L1_LWS: return "MSG_HDR_L1_LWS"; |
| 104 | case HTTP_MSG_HDR_VAL: return "MSG_HDR_VAL"; |
| 105 | case HTTP_MSG_HDR_L2_LF: return "MSG_HDR_L2_LF"; |
| 106 | case HTTP_MSG_HDR_L2_LWS: return "MSG_HDR_L2_LWS"; |
| 107 | case HTTP_MSG_LAST_LF: return "MSG_LAST_LF"; |
| 108 | case HTTP_MSG_ERROR: return "MSG_ERROR"; |
| 109 | case HTTP_MSG_BODY: return "MSG_BODY"; |
| 110 | case HTTP_MSG_100_SENT: return "MSG_100_SENT"; |
| 111 | case HTTP_MSG_CHUNK_SIZE: return "MSG_CHUNK_SIZE"; |
| 112 | case HTTP_MSG_DATA: return "MSG_DATA"; |
| 113 | case HTTP_MSG_CHUNK_CRLF: return "MSG_CHUNK_CRLF"; |
| 114 | case HTTP_MSG_TRAILERS: return "MSG_TRAILERS"; |
| 115 | case HTTP_MSG_ENDING: return "MSG_ENDING"; |
| 116 | case HTTP_MSG_DONE: return "MSG_DONE"; |
| 117 | case HTTP_MSG_CLOSING: return "MSG_CLOSING"; |
| 118 | case HTTP_MSG_CLOSED: return "MSG_CLOSED"; |
| 119 | case HTTP_MSG_TUNNEL: return "MSG_TUNNEL"; |
| 120 | default: return "MSG_??????"; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | |
| 125 | #endif /* _PROTO_H1_H */ |