summaryrefslogtreecommitdiffstats
path: root/kpf/src/Request.h
blob: d31d5ef8f7a7a3bd45f178b44ea89be236a53aae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
  KPF - Public fileserver for KDE

  Copyright 2001 Rik Hemsley (rikkus) <rik@kde.org>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to
  deal in the Software without restriction, including without limitation the
  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  sell copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef KPF_REQUEST_H
#define KPF_REQUEST_H

#include <tqcstring.h>
#include <tqstringlist.h>
#include <tqdatetime.h>

#include "ByteRange.h"

namespace KPF
{
  /**
   * Represents an HTTP request.
   */
  class Request
  {
    public:

      /**
       * HTTP/1.1 specifies many request types, known as 'methods'.
       * An HTTP/1.1 compliant server must implement HEAD and GET.
       * We support only these two. All others are rejected either
       * because they are of limited use or because they are a security
       * risk.
       */
      enum Method { Head, Get, Unsupported };

      /**
       * Construct a Request object and set parameters to defaults. 
       */
      Request();
      virtual ~Request();

      /**
       * Take a list of headers and parse them, setting our values
       * appropriately.
       */
      void parseHeaders(const TQStringList &);

      /**
       * Parse one header line and set whatever value is appropriate.
       */
      void handleHeader(const TQString & name, const TQString & value);

      /**
       * HTTP has had a few revisions (protocols) since birth. Here you may
       * specify the protocol which you believe the client is using. This
       * version of setProtocol parses a string of the form
       * "HTTP/major.minor", where major and minor are integers.
       */
      void setProtocol          (const TQString &);

      /**
       * HTTP has had a few revisions (protocols) since birth. Here you may
       * specify the protocol which you believe the client is using.
       */
      void setProtocol          (uint major, uint minor);

      /**
       * Specify the 'method' requested by the client. This version parses
       * a string.
       */
      void setMethod            (const TQString &);

      /**
       * Specify the 'method' requested by the client.
       */
      void setMethod            (Method);

      /**
       * Set the path to the requested resource. The path is
       * immediately decoded and canonicalised.
       */
      void setPath              (const TQString &);

      /**
       * HTTP/1.1 requests must have a "Host:" header.
       */
      void setHost              (const TQString &);

      /**
       * HTTP/1.1 allows an "If-Modified-Since" header, specifying that
       * the requested resource should only be retrieved if the resource
       * has been modified since a certain date and time.
       */
      void setIfModifiedSince   (const TQDateTime &);

      /**
       * HTTP/1.1 allows an "If-Unmodified-Since" header, specifying that
       * the requested resource should only be retrieved if the resource
       * has NOT been modified since a certain date and time.
       */
      void setIfUnmodifiedSince (const TQDateTime &);

      /**
       * HTTP/1.1 allows an "Expect: 100-continue" header, specifying
       * that the server should immediately respond with "100 Continue".
       */
      void setExpectContinue    (bool);

      /**
       * Specify a range of bytes which should be retrieved from the
       * resource. See RFC 2616.
       */
      void setRange             (const TQString &);

      /**
       * HTTP/1.1 allows "persistent" connections. These may be specified
       * by "Keep-Alive:" or "Connection: Keep-Alive" headers.
       */
      void setPersist           (bool);

      /**
       * @return major version of protocol which was set.
       */
      uint  protocolMajor()         const;

      /**
       * @return minor version of protocol which was set.
       */
      uint  protocolMinor()         const;

      /**
       * @return version of protocol which was set, as major.minor, e.g.
       * if the protocol was set to HTTP/0.9, this would return 0.9.
       */
      float         protocol()              const;

      /**
       * @return the (enumerated) request type ('method').
       */
      Method        method()                const;

      /**
       * @return true if @ref setHost was called previously.
       */
      bool          haveHost()              const;

      /**
       * @return true if @ref setIfModifiedSince was called previously.
       */
      bool          haveIfModifiedSince()   const;

      /**
       * @return true if @ref setIfUnmodifiedSince was called previously.
       */
      bool          haveIfUnmodifiedSince() const;

      /**
       * @return the path that was set with (and modified by) setPath()
       */
      TQString       path()                  const;

      /**
       * @return the host that was set previously.
       */
      TQString       host()                  const;

      /**
       * @return the date/time set by @ref setIfModifiedSince previously.
       */
      TQDateTime     ifModifiedSince()       const;

      /**
       * @return the date/time set by @ref setIfUnmodifiedSince previously.
       */
      TQDateTime     ifUnmodifiedSince()     const;

      /**
       * @return the protocol as a string, e.g. "HTTP/1.1".
       */
      TQCString      protocolString()        const;

      /**
       * @return true if @ref setExpectContinue was used previously.
       */
      bool          expectContinue()        const;

      /**
       * @return byte range set by @ref setRange previously.
       */
      ByteRange     range()                 const;

      /**
       * @return true if @ref setRange was called previously.
       */
      bool          haveRange()             const;

      /**
       * @return true if @ref setPersist was called previously.
       */
      bool          persist()               const;

      /**
       * Reset to initial state.
       */
      void          clear();

      /**
       * Clean up the path given in a request, so it's more like what we
       * expect.
       */
      TQString       clean(const TQString & path) const;

    private:

      // Order dependency
      uint    protocolMajor_;
      uint    protocolMinor_;
      Method  method_;
      bool    haveHost_;
      bool    haveIfModifiedSince_;
      bool    haveIfUnmodifiedSince_;
      bool    expectContinue_;
      bool    haveRange_;
      bool    persist_;
      // End order dependency

      TQString       path_;
      TQString       host_;
      TQDateTime     ifModifiedSince_;
      TQDateTime     ifUnmodifiedSince_;
      ByteRange     range_;
  };

} // End namespace KPF

#endif
// vim:ts=2:sw=2:tw=78:et