fastcgi++
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 {
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:
154  {
155  return m_environment;
156  }
157 
159  std::basic_ostream<charT> out;
160 
162  std::basic_ostream<charT> err;
163 
165 
170  virtual void errorHandler();
171 
173 
179  virtual void bigPostErrorHandler();
180 
183  {
184  return m_role;
185  }
186 
188 
200  const std::function<void(Message)>& callback() const
201  {
202  return m_callback;
203  }
204 
206 
216  virtual bool response() =0;
217 
219 
233  virtual void inHandler(int bytesReceived)
234  {}
235 
237 
251  bool virtual inProcessor()
252  {
253  return false;
254  }
255 
257 
264 
266 
274  void dump(const char* data, size_t size)
275  {
276  m_outStreamBuffer.dump(data, size);
277  }
278 
280 
288  void dump(const unsigned char* data, size_t size)
289  {
290  m_outStreamBuffer.dump(reinterpret_cast<const char*>(data), size);
291  }
292 
294 
303  void dump(std::basic_istream<char>& stream)
304  {
305  m_outStreamBuffer.dump(stream);
306  }
307 
309 
315  unsigned pickLocale(const std::vector<std::string>& locales);
316 
318  void setLocale(const std::string& locale);
319 
320  private:
322 
334  std::function<void(Message)> m_callback;
335 
338 
340  const size_t m_maxPostSize;
341 
344 
347 
349  bool m_kill;
350 
353 
355  void complete();
356 
358  std::function<void(const Socket&, Block&&, bool kill)> m_send;
359 
362 
365 
368 
370  inline const char* codepage() const;
371  };
372 }
373 
374 #endif
Topmost namespace for the fastcgi++ library.
std::mutex m_messagesMutex
Thread safe our message queue.
Definition: request.hpp:79
Protocol::Role m_role
The role that the other side expects this request to play.
Definition: request.hpp:343
std::unique_lock< std::mutex > handler()
Request Handler.
Definition: request.cpp:56
Request handling class
Definition: request.hpp:100
virtual void inHandler(int bytesReceived)
Generate a data input response.
Definition: request.hpp:233
Protocol::ProtocolStatus m_status
Status to end the request with.
Definition: request.hpp:361
virtual bool response()=0
Response generator.
virtual bool inProcessor()
Process custom POST data.
Definition: request.hpp:251
Data structure used to pass messages to requests.
Definition: message.hpp:46
virtual void bigPostErrorHandler()
Called when too much post data is recieved.
Definition: request.cpp:183
void push(Message &&message)
Send a message to the request.
Definition: request.hpp:68
virtual ~Request_base()
Definition: request.hpp:62
std::mutex mutex
Only one thread is allowed to handle the request at a time.
Definition: request.hpp:65
Http::Environment< charT > m_environment
The data structure containing all HTTP environment data.
Definition: request.hpp:337
virtual std::unique_lock< std::mutex > handler()=0
Request Handler.
const size_t m_maxPostSize
The maximum amount of post data, in bytes, that can be recieved.
Definition: request.hpp:340
ProtocolStatus
Possible statuses a request may declare when complete.
Definition: protocol.hpp:171
void dump(const char *data, size_t size)
Dumps raw data directly into the FastCGI protocol.
Definition: request.hpp:274
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:201
De-templating base class for Request.
Definition: request.hpp:45
void dump(std::basic_istream< char > &stream)
Dumps an input stream directly into the FastCGI protocol.
Definition: request.hpp:303
const Http::Environment< charT > & environment() const
Accessor for the HTTP environment data.
Definition: request.hpp:153
Declares everything for relating to the FastCGI protocol itself.
Declares elements of the HTTP protocol.
std::mutex mutex
Thread safe the logging mechanism.
Definition: log.cpp:102
FcgiStreambuf< charT > m_errStreamBuffer
Stream buffer for the err stream.
Definition: request.hpp:367
Class for representing an OS level I/O socket.
Definition: sockets.hpp:83
Data structure of HTTP environment data.
Definition: http.hpp:251
A unique identifier for each FastCGI request.
Definition: protocol.hpp:71
void dump(const unsigned char *data, size_t size)
Dumps raw data directly into the FastCGI protocol.
Definition: request.hpp:288
unsigned pickLocale(const std::vector< std::string > &locales)
Pick a locale.
Definition: request.cpp:226
std::basic_ostream< charT > err
Output stream to the HTTP server error log.
Definition: request.hpp:162
std::basic_ostream< charT > out
Standard output stream to the client.
Definition: request.hpp:159
virtual ~Request()
Definition: request.hpp:149
std::function< void(const Socket &, Block &&, bool kill)> m_send
Function to actually send the record.
Definition: request.hpp:358
Message m_message
The message associated with the current handler() call.
Definition: request.hpp:263
Request(const size_t maxPostSize=0)
Initializes what it can. configure() to finish.
Definition: request.hpp:114
void complete()
Generates an END_REQUEST FastCGI record.
Definition: request.cpp:32
Protocol::RecordType m_state
What the request is current doing.
Definition: request.hpp:352
RecordType
Defines the types of records within the FastCGI protocol.
Definition: protocol.hpp:141
virtual void errorHandler()
Called when a processing error occurs.
Definition: request.cpp:165
const char * codepage() const
Codepage.
void setLocale(const std::string &locale)
Set the output stream&#39;s locale.
Definition: request.cpp:257
std::queue< Message > m_messages
A queue of message for the request.
Definition: request.hpp:76
Declares the FcgiStreambuf class.
const std::function< void(Message)> & callback() const
Callback function for dealings outside the fastcgi++ library.
Definition: request.hpp:200
Protocol::RequestId m_id
The complete ID (request id & file descriptor) associated with the request.
Definition: request.hpp:346
std::function< void(Message)> m_callback
The callback function for dealings outside the fastcgi++ library.
Definition: request.hpp:334
Protocol::Role role() const
See the requests role.
Definition: request.hpp:182
FcgiStreambuf< charT > m_outStreamBuffer
Stream buffer for the out stream.
Definition: request.hpp:364
bool m_kill
Should the socket be closed upon completion.
Definition: request.hpp:349
Role
Defines the possible roles a FastCGI application may play.
Definition: protocol.hpp:163
void dump(const char *data, size_t size)
Dumps raw data directly into the FastCGI protocol.
Data structure to hold a block of raw data.
Definition: block.hpp:44