libtasks Documentation  1.6
uwsgi_request.cpp
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 #include <tasks/logging.h>
9 #include <sys/socket.h>
10 #include <cstdlib>
11 
13 #include <tasks/net/socket.h>
14 
15 namespace tasks {
16 namespace net {
17 
18 std::string uwsgi_request::NO_VAL;
19 
21  std::streamsize bytes = sock.read((char*)&m_header, sizeof(m_header));
22  if (bytes != sizeof(m_header)) {
23  throw tasks_exception(tasks_error::UWSGI_HEADER_ERROR, "uwsgi_request: error reading header");
24  }
25  tdbg("uwsgi_request::read_header: read header successfully, " << bytes << " bytes" << std::endl);
26 }
27 
29  std::streamsize bytes = sock.read(m_data_buffer.ptr_write(), m_data_buffer.to_write());
30  if (bytes > 0) {
32  tdbg("uwsgi_request::read_vars: read data successfully, " << bytes << " bytes" << std::endl);
33  }
34  if (!m_data_buffer.to_write()) {
35  if (UWSGI_VARS == m_header.modifier1) {
36  parse_vars();
37  // Check if a http body needs to be read
38  std::string content_len_s = var("CONTENT_LENGTH");
39  if (NO_VAL != content_len_s) {
40  std::size_t content_len_i = std::atoi(content_len_s.c_str());
41  m_content_buffer.set_size(content_len_i);
43  } else {
45  }
46  }
47  }
48 }
49 
51  std::streamsize bytes = sock.read(m_content_buffer.ptr_write(), m_content_buffer.to_write());
52  if (bytes > 0) {
54  tdbg("uwsgi_request::read_content: read data successfully, " << bytes << " bytes" << std::endl);
55  }
56  if (!m_content_buffer.to_write()) {
58  }
59 }
60 
62  if (io_state::READY == m_state) {
64  read_header(sock);
67  }
69  read_vars(sock);
70  }
72  read_content(sock);
73  }
74 }
75 
77  std::size_t pos = 0;
78  while (pos < m_data_buffer.size()) {
79  uint16_t key_len = *((uint16_t*)m_data_buffer.ptr(pos));
80  uint16_t key_start = pos + 2;
81  uint16_t val_len = *((uint16_t*)m_data_buffer.ptr(key_start + key_len));
82  uint16_t val_start = key_start + key_len + 2;
83  if (key_len && val_len) {
84  m_vars.insert(std::make_pair(std::string(m_data_buffer.ptr(key_start), key_len),
85  std::string(m_data_buffer.ptr(val_start), val_len)));
86  }
87  pos = val_start + val_len;
88  }
89 }
90 
91 } // net
92 } // tasks
void read_data(socket &sock)
Read request data from a socket.
std::size_t size() const
Definition: buffer.h:70
void read_vars(socket &sock)
Read the uwsgi parameters from a socket.
tasks::tools::buffer m_data_buffer
Definition: uwsgi_request.h:98
#define tdbg(m)
Definition: logging.h:54
The socket class.
Definition: socket.h:35
static std::string NO_VAL
Definition: uwsgi_request.h:31
void move_ptr_write(std::size_t s)
Definition: buffer.h:58
char * ptr_write()
Definition: buffer.h:30
tasks::tools::buffer m_content_buffer
Definition: uwsgi_request.h:99
std::streamsize read(char *data, std::size_t len)
Definition: socket.cpp:251
void read_header(socket &sock)
Read the header from a socket.
Tasks execption class.
char * ptr(std::size_t pos)
Definition: buffer.h:36
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 set_size(std::size_t s)
Definition: buffer.h:72
std::streamsize to_write() const
Definition: buffer.h:66
uwsgi_packet_header m_header
Definition: uwsgi_request.h:97
void parse_vars()
Parse the uswgi parameters into a hash map.