/src/uWebSockets/src/MessageParser.h
Line | Count | Source |
1 | | /* |
2 | | * Authored by Alex Hultman, 2018-2020. |
3 | | * Intellectual property of third-party. |
4 | | |
5 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
6 | | * you may not use this file except in compliance with the License. |
7 | | * You may obtain a copy of the License at |
8 | | |
9 | | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | | |
11 | | * Unless required by applicable law or agreed to in writing, software |
12 | | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | | * See the License for the specific language governing permissions and |
15 | | * limitations under the License. |
16 | | */ |
17 | | |
18 | | /* Implements the common parser (RFC 822) used in both HTTP and Multipart parsing */ |
19 | | |
20 | | #ifndef UWS_MESSAGE_PARSER_H |
21 | | #define UWS_MESSAGE_PARSER_H |
22 | | |
23 | | #include <string_view> |
24 | | #include <utility> |
25 | | #include <cstring> |
26 | | |
27 | | /* For now we have this one here */ |
28 | 9.73k | #define MAX_HEADERS 10 |
29 | | |
30 | | namespace uWS { |
31 | | |
32 | | // should be templated on whether it needs at lest one header (http), or not (multipart) |
33 | 3.43k | static inline unsigned int getHeaders(char *postPaddedBuffer, char *end, std::pair<std::string_view, std::string_view> *headers) { |
34 | 3.43k | char *preliminaryKey, *preliminaryValue, *start = postPaddedBuffer; |
35 | | |
36 | 9.73k | for (unsigned int i = 0; i < MAX_HEADERS; i++) { |
37 | 3.41M | for (preliminaryKey = postPaddedBuffer; (*postPaddedBuffer != ':') & (*(unsigned char *)postPaddedBuffer > 32); *(postPaddedBuffer++) |= 32); |
38 | 9.72k | if (*postPaddedBuffer == '\r') { |
39 | 3.36k | if ((postPaddedBuffer != end) & (postPaddedBuffer[1] == '\n') /* & (i > 0) */) { // multipart does not require any headers like http does |
40 | 3.30k | headers->first = std::string_view(nullptr, 0); |
41 | 3.30k | return (unsigned int) ((postPaddedBuffer + 2) - start); |
42 | 3.30k | } else { |
43 | 62 | return 0; |
44 | 62 | } |
45 | 6.36k | } else { |
46 | 6.36k | headers->first = std::string_view(preliminaryKey, (size_t) (postPaddedBuffer - preliminaryKey)); |
47 | 7.77k | for (postPaddedBuffer++; (*postPaddedBuffer == ':' || *(unsigned char *)postPaddedBuffer < 33) && *postPaddedBuffer != '\r'; postPaddedBuffer++); |
48 | 6.36k | preliminaryValue = postPaddedBuffer; |
49 | 6.36k | postPaddedBuffer = (char *) memchr(postPaddedBuffer, '\r', end - postPaddedBuffer); |
50 | 6.36k | if (postPaddedBuffer && postPaddedBuffer[1] == '\n') { |
51 | 6.29k | headers->second = std::string_view(preliminaryValue, (size_t) (postPaddedBuffer - preliminaryValue)); |
52 | 6.29k | postPaddedBuffer += 2; |
53 | 6.29k | headers++; |
54 | 6.29k | } else { |
55 | 68 | return 0; |
56 | 68 | } |
57 | 6.36k | } |
58 | 9.72k | } |
59 | 3 | return 0; |
60 | 3.43k | } |
61 | | |
62 | | } |
63 | | |
64 | | #endif |