1 /* <!-- copyright */
2 /*
3  * aria2 - The high speed download utility
4  *
5  * Copyright (C) 2006 Tatsuhiro Tsujikawa
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program 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
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  * In addition, as a special exception, the copyright holders give
22  * permission to link the code of portions of this program with the
23  * OpenSSL library under certain conditions as described in each
24  * individual source file, and distribute linked combinations
25  * including the two.
26  * You must obey the GNU General Public License in all respects
27  * for all of the code used other than OpenSSL.  If you modify
28  * file(s) with this exception, you may extend this exception to your
29  * version of the file(s), but you are not obligated to do so.  If you
30  * do not wish to do so, delete this exception statement from your
31  * version.  If you delete this exception statement from all source
32  * files in the program, then also delete it here.
33  */
34 /* copyright --> */
35 #ifndef D_HTTP_HEADER_H
36 #define D_HTTP_HEADER_H
37 
38 #include "common.h"
39 
40 #include <map>
41 #include <vector>
42 #include <string>
43 
44 namespace aria2 {
45 
46 struct Range;
47 
48 class HttpHeader {
49 private:
50   std::multimap<int, std::string> table_;
51 
52   // HTTP status code, e.g. 200
53   int statusCode_;
54 
55   // The reason-phrase for the response
56   std::string reasonPhrase_;
57 
58   // HTTP version, e.g. HTTP/1.1
59   std::string version_;
60 
61   // HTTP Method, e.g. GET, POST, etc
62   std::string method_;
63 
64   // Request Path
65   std::string requestPath_;
66 
67 public:
68   HttpHeader();
69   ~HttpHeader();
70 
71   // The list of headers we are interested in. Only those header
72   // values are stored in table_. When updating this list, also update
73   // INTERESTING_HEADER_NAMES in HttpHeader.cc
74   enum InterestingHeader {
75     ACCEPT_ENCODING,
76     ACCESS_CONTROL_REQUEST_HEADERS,
77     ACCESS_CONTROL_REQUEST_METHOD,
78     AUTHORIZATION,
79     CONNECTION,
80     CONTENT_DISPOSITION,
81     CONTENT_ENCODING,
82     CONTENT_LENGTH,
83     CONTENT_RANGE,
84     CONTENT_TYPE,
85     DIGEST,
86     INFOHASH, // Used for BitTorrent LPD
87     LAST_MODIFIED,
88     LINK,
89     LOCATION,
90     ORIGIN,
91     PORT, // Used for BitTorrent LPD
92     RETRY_AFTER,
93     SEC_WEBSOCKET_KEY,
94     SEC_WEBSOCKET_VERSION,
95     SET_COOKIE,
96     TRANSFER_ENCODING,
97     UPGRADE,
98     MAX_INTERESTING_HEADER
99   };
100 
101   // For all methods, use lowercased header field name.
102   void put(int hdKey, const std::string& value);
103   bool defined(int hdKey) const;
104   const std::string& find(int hdKey) const;
105   std::vector<std::string> findAll(int hdKey) const;
106   std::pair<std::multimap<int, std::string>::const_iterator,
107             std::multimap<int, std::string>::const_iterator>
108   equalRange(int hdKey) const;
109 
110   void remove(int hdKey);
111 
112   Range getRange() const;
113 
114   int getStatusCode() const;
115 
116   void setStatusCode(int code);
117 
118   const std::string& getReasonPhrase() const;
119 
120   void setReasonPhrase(const std::string& reasonPhrase);
121 
122   const std::string& getVersion() const;
123 
124   void setVersion(const std::string& version);
125 
126   template <typename InputIterator>
setVersion(InputIterator first,InputIterator last)127   void setVersion(InputIterator first, InputIterator last)
128   {
129     version_.assign(first, last);
130   }
131 
132   const std::string& getMethod() const;
133 
134   void setMethod(const std::string& method);
135 
136   template <typename InputIterator>
setMethod(InputIterator first,InputIterator last)137   void setMethod(InputIterator first, InputIterator last)
138   {
139     method_.assign(first, last);
140   }
141 
142   const std::string& getRequestPath() const;
143 
144   void setRequestPath(const std::string& requestPath);
145 
146   template <typename InputIterator>
setRequestPath(InputIterator first,InputIterator last)147   void setRequestPath(InputIterator first, InputIterator last)
148   {
149     requestPath_.assign(first, last);
150   }
151 
152   // Clears table_. responseStatus_ and version_ are unchanged.
153   void clearField();
154 
155   // Returns true if header field |name| contains |value|. This method
156   // assumes the values of the header field is delimited by ','.
157   bool fieldContains(int hdKey, const char* value);
158 
159   // Returns true if the headers indicate that the remote endpoint
160   // keeps connection open.
161   bool isKeepAlive() const;
162 };
163 
164 int idInterestingHeader(const char* hdName);
165 
166 } // namespace aria2
167 
168 #endif // D_HTTP_HEADER_H
169