Merge pull request #26220 from 78andyp/blurayfixes
[xbmc.git] / xbmc / utils / HttpParser.h
blob47a3a9fc731ec09ba1eef47f202f4c90fa39c968
1 /*
2 * Copyright (C) 2011-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
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
14 #pragma once
16 #include <stdlib.h>
17 #include <string.h>
18 #include <string>
19 #include <vector>
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".
26 // Example usage:
28 // HttpParser parser;
29 // HttpParser::status_t status;
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 // }
37 // if ( status == HttpParser::Done ) {
38 // // parse fully formed http message.
39 // }
42 class HttpParser
44 public:
45 ~HttpParser();
47 enum status_t {
48 Done,
49 Error,
50 Incomplete
53 status_t addBytes( const char* bytes, unsigned len );
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;
63 private:
64 void parseHeader();
65 bool parseRequestLine();
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;
77 typedef std::vector<unsigned> IntArray;
78 IntArray _keys;
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.
96 status_t _status = Incomplete ;