/src/wt/src/http/Request.h
Line | Count | Source |
1 | | // This may look like C code, but it's really -*- C++ -*- |
2 | | /* |
3 | | * Copyright (C) 2008 Emweb bv, Herent, Belgium. |
4 | | * |
5 | | * All rights reserved. |
6 | | */ |
7 | | // |
8 | | // request.hpp |
9 | | // ~~~~~~~~~~~ |
10 | | // |
11 | | // Copyright (c) 2003-2006 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
12 | | // |
13 | | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
14 | | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
15 | | // |
16 | | |
17 | | #ifndef HTTP_REQUEST_HPP |
18 | | #define HTTP_REQUEST_HPP |
19 | | |
20 | | #include <string> |
21 | | #include <map> |
22 | | #include <vector> |
23 | | #include <boost/cstdint.hpp> |
24 | | #include <boost/algorithm/string.hpp> |
25 | | |
26 | | // For ::int64_ and ::uint64_t on Windows only |
27 | | #include "Wt/WDllDefs.h" |
28 | | |
29 | | #ifdef HTTP_WITH_SSL |
30 | | #include <openssl/ssl.h> |
31 | | #endif |
32 | | |
33 | | namespace Wt { |
34 | | class WSslInfo; |
35 | | } |
36 | | |
37 | | namespace http { |
38 | | namespace server { |
39 | | |
40 | | /* |
41 | | * string that references data in our connection receive buffers, |
42 | | * taking into account that a string may span several buffer |
43 | | * boundaries |
44 | | */ |
45 | | struct buffer_string |
46 | | { |
47 | | char *data; |
48 | | unsigned int len; |
49 | | buffer_string *next; |
50 | | |
51 | 15.4k | buffer_string() : data(nullptr), len(0), next(nullptr) { } |
52 | | |
53 | 5.25k | bool empty() const { return len == 0 && (!next || next->empty()); } |
54 | 2.20k | void clear() { data = nullptr; len = 0; next = nullptr; } |
55 | | std::string str() const; |
56 | | unsigned length() const; |
57 | | bool contains(const char *s) const; |
58 | | bool icontains(const char *s) const; |
59 | | bool iequals(const char *s) const; |
60 | | |
61 | | template<unsigned int N> |
62 | 0 | bool istarts_with(const char (&str)[N]) const { |
63 | 0 | return istarts_with(str, N-1); |
64 | 0 | } |
65 | | |
66 | | void write(std::ostream &os) const; |
67 | | |
68 | | bool operator==(const buffer_string& other) const; |
69 | | bool operator==(const std::string& other) const; |
70 | | bool operator==(const char *other) const; |
71 | | bool operator!=(const char *other) const; |
72 | | |
73 | | private: |
74 | | bool istarts_with(const char *s, unsigned int len) const; |
75 | | }; |
76 | | |
77 | | std::ostream& operator<< (std::ostream &os, const buffer_string &str); |
78 | | |
79 | | /// A request received from a client. |
80 | | /// A request with a body will have a content-length. |
81 | | class Request |
82 | | { |
83 | | public: |
84 | | struct Header { |
85 | | buffer_string name; |
86 | | buffer_string value; |
87 | | }; |
88 | | |
89 | | #ifdef WTHTTP_WITH_ZLIB |
90 | | struct PerMessageDeflateState { |
91 | | bool enabled; |
92 | | int client_max_window_bits; // -1 means no context takeover |
93 | | int server_max_window_bits; // -1 means no context takeover |
94 | | }; |
95 | | #endif |
96 | | |
97 | 1.96k | Request() { |
98 | 1.96k | extra_start_index = 0; |
99 | | #ifdef HTTP_WITH_SSL |
100 | | ssl = nullptr; |
101 | | #endif |
102 | 1.96k | http_version_major = -1; |
103 | 1.96k | http_version_minor = -1; |
104 | 1.96k | } |
105 | | enum State { Partial, Complete, Error }; |
106 | | |
107 | | buffer_string method; |
108 | | buffer_string uri; |
109 | | char urlScheme[10]; |
110 | | std::string remoteIP; |
111 | | short port; |
112 | | int http_version_major; |
113 | | int http_version_minor; |
114 | | |
115 | | typedef std::list<Header> HeaderList; |
116 | | HeaderList headers; |
117 | | ::int64_t contentLength; |
118 | | int webSocketVersion; |
119 | | #ifdef WTHTTP_WITH_ZLIB |
120 | | mutable PerMessageDeflateState pmdState_; |
121 | | #endif |
122 | | |
123 | | enum Type { |
124 | | HTTP, // HTTP request |
125 | | WebSocket, // WebSocket request |
126 | | TCP // Raw TCP request (not interpreted by parser, e.g. for proxying) |
127 | | } type; |
128 | | |
129 | | std::string request_path; |
130 | | std::string request_query; |
131 | | std::size_t extra_start_index; |
132 | | |
133 | | std::vector<std::pair<std::string, std::string> > url_params; |
134 | | |
135 | | #ifdef HTTP_WITH_SSL |
136 | | SSL *ssl; |
137 | | #endif |
138 | | std::unique_ptr<Wt::WSslInfo> sslInfo() const; |
139 | | |
140 | | void reset(); |
141 | | void process(); |
142 | | |
143 | | bool closeConnection() const; |
144 | | bool acceptGzipEncoding() const; |
145 | | void enableWebSocket(); |
146 | | const Header *getHeader(const std::string& name) const; |
147 | | const Header *getHeader(const char *name) const; |
148 | | }; |
149 | | |
150 | | } // namespace server |
151 | | } // namespace http |
152 | | |
153 | | #endif // HTTP_REQUEST_HPP |