diff options
Diffstat (limited to 'xbmc/utils/HttpParser.h')
| -rw-r--r-- | xbmc/utils/HttpParser.h | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/xbmc/utils/HttpParser.h b/xbmc/utils/HttpParser.h new file mode 100644 index 0000000..47a3a9f --- /dev/null +++ b/xbmc/utils/HttpParser.h | |||
| @@ -0,0 +1,98 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2011-2018 Team Kodi | ||
| 3 | * This file is part of Kodi - https://kodi.tv | ||
| 4 | * | ||
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later | ||
| 6 | * See LICENSES/README.md for more information. | ||
| 7 | * | ||
| 8 | * This code implements parsing of HTTP requests. | ||
| 9 | * This code was written by Steve Hanov in 2009, no copyright is claimed. | ||
| 10 | * This code is in the public domain. | ||
| 11 | * Code was taken from http://refactormycode.com/codes/778-an-efficient-http-parser | ||
| 12 | */ | ||
| 13 | |||
| 14 | #pragma once | ||
| 15 | |||
| 16 | #include <stdlib.h> | ||
| 17 | #include <string.h> | ||
| 18 | #include <string> | ||
| 19 | #include <vector> | ||
| 20 | |||
| 21 | // A class to incrementally parse an HTTP header as it comes in. It | ||
| 22 | // lets you know when it has received all required bytes, as specified | ||
| 23 | // by the content-length header (if present). If there is no content-length, | ||
| 24 | // it will stop reading after the final "\n\r". | ||
| 25 | // | ||
| 26 | // Example usage: | ||
| 27 | // | ||
| 28 | // HttpParser parser; | ||
| 29 | // HttpParser::status_t status; | ||
| 30 | // | ||
| 31 | // for( ;; ) { | ||
| 32 | // // read bytes from socket into buffer, break on error | ||
| 33 | // status = parser.addBytes( buffer, length ); | ||
| 34 | // if ( status != HttpParser::Incomplete ) break; | ||
| 35 | // } | ||
| 36 | // | ||
| 37 | // if ( status == HttpParser::Done ) { | ||
| 38 | // // parse fully formed http message. | ||
| 39 | // } | ||
| 40 | |||
| 41 | |||
| 42 | class HttpParser | ||
| 43 | { | ||
| 44 | public: | ||
| 45 | ~HttpParser(); | ||
| 46 | |||
| 47 | enum status_t { | ||
| 48 | Done, | ||
| 49 | Error, | ||
| 50 | Incomplete | ||
| 51 | }; | ||
| 52 | |||
| 53 | status_t addBytes( const char* bytes, unsigned len ); | ||
| 54 | |||
| 55 | const char* getMethod() const; | ||
| 56 | const char* getUri() const; | ||
| 57 | const char* getQueryString() const; | ||
| 58 | const char* getBody() const; | ||
| 59 | // key should be in lower case when looking up. | ||
| 60 | const char* getValue( const char* key ) const; | ||
| 61 | unsigned getContentLength() const; | ||
| 62 | |||
| 63 | private: | ||
| 64 | void parseHeader(); | ||
| 65 | bool parseRequestLine(); | ||
| 66 | |||
| 67 | std::string _data; | ||
| 68 | unsigned _headerStart = 0; | ||
| 69 | unsigned _parsedTo = 0 ; | ||
| 70 | int _state = 0 ; | ||
| 71 | unsigned _keyIndex = 0; | ||
| 72 | unsigned _valueIndex = 0; | ||
| 73 | unsigned _contentLength = 0; | ||
| 74 | unsigned _contentStart = 0; | ||
| 75 | unsigned _uriIndex = 0; | ||
| 76 | |||
| 77 | typedef std::vector<unsigned> IntArray; | ||
| 78 | IntArray _keys; | ||
| 79 | |||
| 80 | enum State { | ||
| 81 | p_request_line=0, | ||
| 82 | p_request_line_cr=1, | ||
| 83 | p_request_line_crlf=2, | ||
| 84 | p_request_line_crlfcr=3, | ||
| 85 | p_key=4, | ||
| 86 | p_key_colon=5, | ||
| 87 | p_key_colon_sp=6, | ||
| 88 | p_value=7, | ||
| 89 | p_value_cr=8, | ||
| 90 | p_value_crlf=9, | ||
| 91 | p_value_crlfcr=10, | ||
| 92 | p_content=11, // here we are done parsing the header. | ||
| 93 | p_error=12 // here an error has occurred and the parse failed. | ||
| 94 | }; | ||
| 95 | |||
| 96 | status_t _status = Incomplete ; | ||
| 97 | }; | ||
| 98 | |||
