fastcgi++  3.1alpha
A C++ FastCGI/Web API
request.hpp
Go to the documentation of this file.
1 
10 /*******************************************************************************
11 * Copyright (C) 2017 Eddie Carle [eddie@isatec.ca] *
12 * *
13 * This file is part of fastcgi++. *
14 * *
15 * fastcgi++ is free software: you can redistribute it and/or modify it under *
16 * the terms of the GNU Lesser General Public License as published by the Free *
17 * Software Foundation, either version 3 of the License, or (at your option) *
18 * any later version. *
19 * *
20 * fastcgi++ is distributed in the hope that it will be useful, but WITHOUT ANY *
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for *
23 * more details. *
24 * *
25 * You should have received a copy of the GNU Lesser General Public License *
26 * along with fastcgi++. If not, see <http://www.gnu.org/licenses/>. *
27 *******************************************************************************/
28 
29 #ifndef FASTCGIPP_REQUEST_HPP
30 #define FASTCGIPP_REQUEST_HPP
31 
32 #include "fastcgi++/protocol.hpp"
34 #include "fastcgi++/http.hpp"
35 
36 #include <ostream>
37 #include <functional>
38 #include <queue>
39 #include <mutex>
40 
42 namespace Fastcgipp
43 {
45  class Request_base
46  {
47  public:
49 
60  virtual std::unique_lock<std::mutex> handler() =0;
61 
62  virtual ~Request_base() {}
63 
66 
68  inline void push(Message&& message)
69  {
70  std::lock_guard<std::mutex> lock(m_messagesMutex);
71  m_messages.push(std::move(message));
72  }
73 
74  protected:
76  std::queue<Message> m_messages;
77 
80  };
81 
83 
100  template<class charT> class Request: public Request_base
101  {
102  public:
104 
114  Request(const size_t maxPostSize=0):
117  m_maxPostSize(maxPostSize),
118  m_state(Protocol::RecordType::PARAMS),
119  m_status(Protocol::ProtocolStatus::REQUEST_COMPLETE)
120  {
121  out.imbue(std::locale("C"));
122  err.imbue(std::locale("C"));
123  }
124 
126 
139  void configure(
140  const Protocol::RequestId& id,
141  const Protocol::Role& role,
142  bool kill,
143  const std::function<void(const Socket&, Block&&, bool)>
144  send,
145  const std::function<void(Message)> callback);
146 
147  std::unique_lock<std::mutex> handler();
148 
149  virtual ~Request() {}
150 
151  protected:
153  const Http::Environment<charT>& environment() const
154  {
155  return m_environment;
156  }
157 
159  Http::Environment<charT>& environment()
160  {
161  return m_environment;
162  }
163 
165  std::basic_ostream<charT> out;
166 
168  std::basic_ostream<charT> err;
169 
171 
176  virtual void errorHandler();
177 
179 
185  virtual void bigPostErrorHandler();
186 
188 
193  virtual void unknownContentErrorHandler();
194 
196  Protocol::Role role() const
197  {
198  return m_role;
199  }
200 
202 
214  const std::function<void(Message)>& callback() const
215  {
216  return m_callback;
217  }
218 
220 
230  virtual bool response() =0;
231 
233 
247  virtual void inHandler(int bytesReceived)
248  {}
249 
251 
265  virtual bool inProcessor()
266  {
267  return false;
268  }
269 
271 
277  Message m_message;
278 
280 
288  void dump(const char* data, size_t size)
289  {
290  m_outStreamBuffer.dump(data, size);
291  }
292 
294 
302  void dump(const unsigned char* data, size_t size)
303  {
304  m_outStreamBuffer.dump(reinterpret_cast<const char*>(data), size);
305  }
306 
308 
317  void dump(std::basic_istream<char>& stream)
318  {
320  }
321 
323 
329  unsigned pickLocale(const std::vector<std::string>& locales);
330 
332  void setLocale(const std::string& locale);
333 
334  private:
336 
348  std::function<void(Message)> m_callback;
349 
352 
354  const size_t m_maxPostSize;
355 
358 
361 
363  bool m_kill;
364 
367 
369  void complete();
370 
372  std::function<void(const Socket&, Block&&, bool kill)> m_send;
373 
376 
379 
382 
384  inline const char* codepage() const;
385  };
386 }
387 
388 #endif
Fastcgipp::Request::setLocale
void setLocale(const std::string &locale)
Set the output stream's locale.
Definition: request.cpp:275
Fastcgipp::Request::m_environment
Http::Environment< charT > m_environment
The data structure containing all HTTP environment data.
Definition: request.hpp:368
Fastcgipp::Protocol::Role
Role
Defines the possible roles a FastCGI application may play.
Definition: protocol.hpp:180
Fastcgipp::Request_base::m_messages
std::queue< Message > m_messages
A queue of message for the request.
Definition: request.hpp:110
Fastcgipp::Request::unknownContentErrorHandler
virtual void unknownContentErrorHandler()
Called when receiving an unknown content type.
Definition: request.cpp:203
Fastcgipp::Request::err
std::basic_ostream< charT > err
Output stream to the HTTP server error log.
Definition: request.hpp:185
Fastcgipp::Request::role
Protocol::Role role() const
See the requests role.
Definition: request.hpp:213
Fastcgipp::Block
Data structure to hold a block of raw data.
Definition: block.hpp:61
Fastcgipp::Protocol::ProtocolStatus
ProtocolStatus
Possible statuses a request may declare when complete.
Definition: protocol.hpp:188
Fastcgipp::Request::complete
void complete()
Generates an END_REQUEST FastCGI record.
Definition: request.cpp:32
Fastcgipp::Logging::mutex
std::mutex mutex
Thread safe the logging mechanism.
Definition: log.cpp:102
Fastcgipp::Message
Data structure used to pass messages to requests.
Definition: message.hpp:63
Fastcgipp::Request::handler
std::unique_lock< std::mutex > handler()
Request Handler.
Definition: request.cpp:56
Fastcgipp::Request::m_state
Protocol::RecordType m_state
What the request is current doing.
Definition: request.hpp:383
Fastcgipp::Request::codepage
const char * codepage() const
Codepage.
fcgistreambuf.hpp
Declares the FcgiStreambuf class.
Fastcgipp::Request::dump
void dump(const char *data, size_t size)
Dumps raw data directly into the FastCGI protocol.
Definition: request.hpp:305
Fastcgipp::Request::environment
const Http::Environment< charT > & environment() const
Const accessor for the HTTP environment data.
Definition: request.hpp:170
Fastcgipp::Request::m_maxPostSize
const size_t m_maxPostSize
The maximum amount of post data, in bytes, that can be recieved.
Definition: request.hpp:371
Fastcgipp::Request::bigPostErrorHandler
virtual void bigPostErrorHandler()
Called when too much post data is recieved.
Definition: request.cpp:187
Fastcgipp::Request::~Request
virtual ~Request()
Definition: request.hpp:166
Fastcgipp::Request::m_role
Protocol::Role m_role
The role that the other side expects this request to play.
Definition: request.hpp:374
Fastcgipp::Request::pickLocale
unsigned pickLocale(const std::vector< std::string > &locales)
Pick a locale.
Definition: request.cpp:244
Fastcgipp::Request::m_id
Protocol::RequestId m_id
The complete ID (request id & file descriptor) associated with the request.
Definition: request.hpp:377
Fastcgipp::Request::m_outStreamBuffer
FcgiStreambuf< charT > m_outStreamBuffer
Stream buffer for the out stream.
Definition: request.hpp:395
Fastcgipp::Request::callback
const std::function< void(Message)> & callback() const
Callback function for dealings outside the fastcgi++ library.
Definition: request.hpp:231
Fastcgipp::Request::errorHandler
virtual void errorHandler()
Called when a processing error occurs.
Definition: request.cpp:171
Fastcgipp::Http::Environment
Data structure of HTTP environment data.
Definition: http.hpp:156
Fastcgipp::Request::m_kill
bool m_kill
Should the socket be closed upon completion.
Definition: request.hpp:380
Fastcgipp::Request::response
virtual bool response()=0
Response generator.
http.hpp
Declares the Address class.
Fastcgipp::Request::Request
Request(const size_t maxPostSize=0)
Initializes what it can. configure() to finish.
Definition: request.hpp:131
Fastcgipp::Socket
Class for representing an OS level I/O socket.
Definition: sockets.hpp:79
Fastcgipp::Request_base::mutex
std::mutex mutex
Only one thread is allowed to handle the request at a time.
Definition: request.hpp:99
Fastcgipp::Request::m_send
std::function< void(const Socket &, Block &&, bool kill)> m_send
Function to actually send the record.
Definition: request.hpp:389
Fastcgipp::Request::inProcessor
virtual bool inProcessor()
Process custom POST data.
Definition: request.hpp:282
Fastcgipp::FcgiStreambuf::dump
void dump(const char *data, size_t size)
Dumps raw data directly into the FastCGI protocol.
Definition: fcgistreambuf.cpp:131
Fastcgipp::Request::inHandler
virtual void inHandler(int bytesReceived)
Generate a data input response.
Definition: request.hpp:264
Fastcgipp::Request::m_message
Message m_message
The message associated with the current handler() call.
Definition: request.hpp:294
Fastcgipp
Topmost namespace for the fastcgi++ library.
Definition: fcgistreambuf.cpp:34
protocol.hpp
Declares everything for relating to the FastCGI protocol itself.
Fastcgipp::Protocol::RecordType
RecordType
Defines the types of records within the FastCGI protocol.
Definition: protocol.hpp:158
Fastcgipp::Request::m_errStreamBuffer
FcgiStreambuf< charT > m_errStreamBuffer
Stream buffer for the err stream.
Definition: request.hpp:398
Fastcgipp::Request::m_callback
std::function< void(Message)> m_callback
The callback function for dealings outside the fastcgi++ library.
Definition: request.hpp:365
Fastcgipp::Protocol::RequestId
A unique identifier for each FastCGI request.
Definition: protocol.hpp:88
Fastcgipp::Request::out
std::basic_ostream< charT > out
Standard output stream to the client.
Definition: request.hpp:182
Fastcgipp::Request_base::push
void push(Message &&message)
Send a message to the request.
Definition: request.hpp:102
Fastcgipp::Request::configure
void configure(const Protocol::RequestId &id, const Protocol::Role &role, bool kill, const std::function< void(const Socket &, Block &&, bool)> send, const std::function< void(Message)> callback)
Configures the request with the data it needs.
Definition: request.cpp:219
Fastcgipp::Request_base::m_messagesMutex
std::mutex m_messagesMutex
Thread safe our message queue.
Definition: request.hpp:113
Fastcgipp::Request::m_status
Protocol::ProtocolStatus m_status
Status to end the request with.
Definition: request.hpp:392
Fastcgipp::FcgiStreambuf< charT >
Fastcgipp::Request_base::handler
virtual std::unique_lock< std::mutex > handler()=0
Request Handler.
Fastcgipp::Request_base::~Request_base
virtual ~Request_base()
Definition: request.hpp:96