libtasks Documentation  1.6
http_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 
9 
10 namespace tasks {
11 namespace net {
12 
13 void http_request::init_default_headers() { m_headers["User-Agent"] = "libtasks"; }
14 
16  assert(m_url.length() > 0);
17  std::string ctlen;
18  // GET/POST
19  if (m_content_buffer.size()) {
20  m_data_buffer.write("POST ", 5);
21  ctlen = "Content-Length: " + std::to_string(m_content_buffer.size());
22  } else {
23  m_data_buffer.write("GET ", 4);
24  }
25  m_data_buffer.write(m_url.c_str(), m_url.length());
26  m_data_buffer.write(" HTTP/1.1", 9);
28  // Headers
29  for (auto& kv : m_headers) {
30  m_data_buffer.write(kv.first.c_str(), kv.first.length());
31  m_data_buffer.write(": ", 2);
32  m_data_buffer.write(kv.second.c_str(), kv.second.length());
34  }
35  // Content length
36  m_data_buffer.write(ctlen.c_str(), ctlen.length());
37  // End
40 }
41 
42 } // net
43 } // tasks
tasks::tools::buffer m_data_buffer
Definition: http_base.h:130
std::size_t size() const
Definition: buffer.h:70
#define CRLF
Definition: http_base.h:24
std::unordered_map< std::string, std::string > m_headers
Definition: http_base.h:133
tasks::tools::buffer m_content_buffer
Definition: http_base.h:131
std::streamsize write(const char_type *data, std::streamsize size)
Definition: buffer.h:91
void prepare_data_buffer()
Prepare a HTTP request/response to be sent.
#define CRLF_SIZE
Definition: http_base.h:25