libtasks Documentation  1.6
uwsgi_request.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2014 ADTECH GmbH
3  * Licensed under MIT (https://github.com/adtechlabs/libtasks/blob/master/COPYING)
4  *
5  * Author: Andreas Pohl
6  */
7 
8 #ifndef _UWSGI_REQUEST_H_
9 #define _UWSGI_REQUEST_H_
10 
11 #include <string>
12 #include <cstring>
13 #include <unordered_map>
14 #include <vector>
15 #include <iostream>
16 
17 #include <tasks/tasks_exception.h>
18 #include <tasks/net/socket.h>
20 #include <tasks/net/io_state.h>
21 #include <tasks/tools/buffer.h>
22 
23 namespace tasks {
24 namespace net {
25 
28  public:
29  using uwsgi_vars_t = std::unordered_map<std::string, std::string>;
30 
31  static std::string NO_VAL;
32 
33  uwsgi_request() { m_header = {0, 0, 0}; }
34 
39  inline const std::string& var(std::string key) const {
40  auto it = m_vars.find(key);
41  if (m_vars.end() != it) {
42  return it->second;
43  }
44  return NO_VAL;
45  }
46 
48  inline const uwsgi_vars_t& vars() const { return m_vars; }
49 
50  inline void print_header() const {
51  std::cout << "header:"
52  << " modifier1=" << (int)m_header.modifier1 << " datasize=" << m_header.datasize
53  << " modifier2=" << (int)m_header.modifier2 << std::endl;
54  }
55 
56  inline void print_vars() const {
57  for (auto& kv : m_vars) {
58  std::cout << kv.first << " = " << kv.second << std::endl;
59  }
60  }
61 
66  inline void write(const char* data, std::size_t size) { m_content_buffer.write(data, size); }
67 
73  inline std::streamsize read(char* data, std::size_t size) { return m_content_buffer.read(data, size); }
74 
76  void read_data(socket& sock);
77 
80  inline bool done() const { return m_state == io_state::DONE; }
81 
83  inline uwsgi_packet_header& header() { return m_header; }
84 
86  inline void clear() {
88  m_header = {0, 0, 0};
91  if (m_vars.size()) {
92  m_vars.clear();
93  }
94  }
95 
96  private:
97  uwsgi_packet_header m_header;
102 
104  void read_header(socket& sock);
105 
107  void read_vars(socket& sock);
108 
110  void read_content(socket& sock);
111 
113  void parse_vars();
114 };
115 
116 } // net
117 } // tasks
118 
119 #endif // _UWSGI_REQUEST_H_
void read_data(socket &sock)
Read request data from a socket.
void read_vars(socket &sock)
Read the uwsgi parameters from a socket.
tasks::tools::buffer m_data_buffer
Definition: uwsgi_request.h:98
std::unordered_map< std::string, std::string > uwsgi_vars_t
Definition: uwsgi_request.h:29
std::streamsize read(char *data, std::size_t size)
Definition: uwsgi_request.h:73
void print_header() const
Definition: uwsgi_request.h:50
The socket class.
Definition: socket.h:35
static std::string NO_VAL
Definition: uwsgi_request.h:31
std::streamsize read(char_type *data, std::streamsize size)
Definition: buffer.h:93
tasks::tools::buffer m_content_buffer
Definition: uwsgi_request.h:99
void read_header(socket &sock)
Read the header from a socket.
const uwsgi_vars_t & vars() const
Definition: uwsgi_request.h:48
The uwsgi protocol implementation for a request. The response is a HTTP/1.1 response.
Definition: uwsgi_request.h:27
std::streamsize write(const char_type *data, std::streamsize size)
Definition: buffer.h:91
uwsgi_packet_header & header()
Definition: uwsgi_request.h:83
void read_content(socket &sock)
Read POST data into the content buffer.
const std::string & var(std::string key) const
Definition: uwsgi_request.h:39
void clear()
Reset an object.
Definition: uwsgi_request.h:86
uwsgi_packet_header m_header
Definition: uwsgi_request.h:97
void write(const char *data, std::size_t size)
Definition: uwsgi_request.h:66
void parse_vars()
Parse the uswgi parameters into a hash map.