qBittorrent
Http Namespace Reference

Classes

class  Connection
 
class  IRequestHandler
 
class  RequestParser
 
class  ResponseBuilder
 
class  Server
 
struct  Environment
 
struct  UploadedFile
 
struct  Header
 
struct  Request
 
struct  ResponseStatus
 
struct  Response
 

Typedefs

using HeaderMap = QMap< QString, QString >
 

Functions

QByteArray toByteArray (Response response)
 
QString httpDate ()
 
void compressContent (Response &response)
 

Variables

const char METHOD_GET [] = "GET"
 
const char METHOD_POST [] = "POST"
 
const char HEADER_CACHE_CONTROL [] = "cache-control"
 
const char HEADER_CONNECTION [] = "connection"
 
const char HEADER_CONTENT_DISPOSITION [] = "content-disposition"
 
const char HEADER_CONTENT_ENCODING [] = "content-encoding"
 
const char HEADER_CONTENT_LENGTH [] = "content-length"
 
const char HEADER_CONTENT_SECURITY_POLICY [] = "content-security-policy"
 
const char HEADER_CONTENT_TYPE [] = "content-type"
 
const char HEADER_DATE [] = "date"
 
const char HEADER_HOST [] = "host"
 
const char HEADER_ORIGIN [] = "origin"
 
const char HEADER_REFERER [] = "referer"
 
const char HEADER_REFERRER_POLICY [] = "referrer-policy"
 
const char HEADER_SET_COOKIE [] = "set-cookie"
 
const char HEADER_X_CONTENT_TYPE_OPTIONS [] = "x-content-type-options"
 
const char HEADER_X_FORWARDED_FOR [] = "x-forwarded-for"
 
const char HEADER_X_FORWARDED_HOST [] = "x-forwarded-host"
 
const char HEADER_X_FRAME_OPTIONS [] = "x-frame-options"
 
const char HEADER_X_XSS_PROTECTION [] = "x-xss-protection"
 
const char HEADER_REQUEST_METHOD_GET [] = "GET"
 
const char HEADER_REQUEST_METHOD_HEAD [] = "HEAD"
 
const char HEADER_REQUEST_METHOD_POST [] = "POST"
 
const char CONTENT_TYPE_HTML [] = "text/html"
 
const char CONTENT_TYPE_CSS [] = "text/css"
 
const char CONTENT_TYPE_TXT [] = "text/plain; charset=UTF-8"
 
const char CONTENT_TYPE_JS [] = "application/javascript"
 
const char CONTENT_TYPE_JSON [] = "application/json"
 
const char CONTENT_TYPE_GIF [] = "image/gif"
 
const char CONTENT_TYPE_PNG [] = "image/png"
 
const char CONTENT_TYPE_FORM_ENCODED [] = "application/x-www-form-urlencoded"
 
const char CONTENT_TYPE_FORM_DATA [] = "multipart/form-data"
 
const char CRLF [] = {0x0D, 0x0A, '\0'}
 

Typedef Documentation

◆ HeaderMap

using Http::HeaderMap = typedef QMap<QString, QString>

Definition at line 99 of file types.h.

Function Documentation

◆ compressContent()

void Http::compressContent ( Response response)

Definition at line 81 of file responsegenerator.cpp.

82 {
83  if (response.headers.value(HEADER_CONTENT_ENCODING) != QLatin1String("gzip"))
84  return;
85 
86  response.headers.remove(HEADER_CONTENT_ENCODING);
87 
88  // for very small files, compressing them only wastes cpu cycles
89  const int contentSize = response.content.size();
90  if (contentSize <= 1024) // 1 kb
91  return;
92 
93  // filter out known hard-to-compress types
94  const QString contentType = response.headers[HEADER_CONTENT_TYPE];
95  if ((contentType == CONTENT_TYPE_GIF) || (contentType == CONTENT_TYPE_PNG))
96  return;
97 
98  // try compressing
99  bool ok = false;
100  const QByteArray compressedData = Utils::Gzip::compress(response.content, 6, &ok);
101  if (!ok)
102  return;
103 
104  // "Content-Encoding: gzip\r\n" is 24 bytes long
105  if ((compressedData.size() + 24) >= contentSize)
106  return;
107 
108  response.content = compressedData;
109  response.headers[HEADER_CONTENT_ENCODING] = QLatin1String("gzip");
110 }
const char CONTENT_TYPE_PNG[]
Definition: types.h:70
const char HEADER_CONTENT_TYPE[]
Definition: types.h:47
const char HEADER_CONTENT_ENCODING[]
Definition: types.h:44
const char CONTENT_TYPE_GIF[]
Definition: types.h:69
QByteArray compress(const QByteArray &data, int level=6, bool *ok=nullptr)
Definition: gzip.cpp:41

References Utils::Gzip::compress(), Http::Response::content, CONTENT_TYPE_GIF, CONTENT_TYPE_PNG, HEADER_CONTENT_ENCODING, HEADER_CONTENT_TYPE, and Http::Response::headers.

Referenced by toByteArray().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ httpDate()

QString Http::httpDate ( )

Definition at line 72 of file responsegenerator.cpp.

73 {
74  // [RFC 7231] 7.1.1.1. Date/Time Formats
75  // example: "Sun, 06 Nov 1994 08:49:37 GMT"
76 
77  return QLocale::c().toString(QDateTime::currentDateTimeUtc(), QLatin1String("ddd, dd MMM yyyy HH:mm:ss"))
78  .append(QLatin1String(" GMT"));
79 }

Referenced by toByteArray().

Here is the caller graph for this function:

◆ toByteArray()

QByteArray Http::toByteArray ( Response  response)

Definition at line 37 of file responsegenerator.cpp.

38 {
39  compressContent(response);
40 
41  response.headers[HEADER_CONTENT_LENGTH] = QString::number(response.content.length());
42  response.headers[HEADER_DATE] = httpDate();
43 
44  QByteArray buf;
45  buf.reserve(1024 + response.content.length());
46 
47  // Status Line
48  buf.append("HTTP/1.1 ") // TODO: depends on request
49  .append(QByteArray::number(response.status.code))
50  .append(' ')
51  .append(response.status.text.toLatin1())
52  .append(CRLF);
53 
54  // Header Fields
55  for (auto i = response.headers.constBegin(); i != response.headers.constEnd(); ++i)
56  {
57  buf.append(i.key().toLatin1())
58  .append(": ")
59  .append(i.value().toLatin1())
60  .append(CRLF);
61  }
62 
63  // the first empty line
64  buf += CRLF;
65 
66  // message body // TODO: support HEAD request
67  buf += response.content;
68 
69  return buf;
70 }
void compressContent(Response &response)
const char CRLF[]
Definition: types.h:75
const char HEADER_CONTENT_LENGTH[]
Definition: types.h:45
const char HEADER_DATE[]
Definition: types.h:48
QString httpDate()

References Http::ResponseStatus::code, compressContent(), Http::Response::content, CRLF, HEADER_CONTENT_LENGTH, HEADER_DATE, Http::Response::headers, httpDate(), Http::Response::status, and Http::ResponseStatus::text.

Referenced by Http::Connection::sendResponse().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ CONTENT_TYPE_CSS

const char Http::CONTENT_TYPE_CSS[] = "text/css"
inline

Definition at line 65 of file types.h.

Referenced by anonymous_namespace{webapplication.cpp}::getCachingInterval().

◆ CONTENT_TYPE_FORM_DATA

const char Http::CONTENT_TYPE_FORM_DATA[] = "multipart/form-data"
inline

Definition at line 72 of file types.h.

Referenced by Http::RequestParser::parsePostMessage().

◆ CONTENT_TYPE_FORM_ENCODED

const char Http::CONTENT_TYPE_FORM_ENCODED[] = "application/x-www-form-urlencoded"
inline

Definition at line 71 of file types.h.

Referenced by Http::RequestParser::parsePostMessage().

◆ CONTENT_TYPE_GIF

const char Http::CONTENT_TYPE_GIF[] = "image/gif"
inline

Definition at line 69 of file types.h.

Referenced by compressContent().

◆ CONTENT_TYPE_HTML

const char Http::CONTENT_TYPE_HTML[] = "text/html"
inline

Definition at line 64 of file types.h.

◆ CONTENT_TYPE_JS

const char Http::CONTENT_TYPE_JS[] = "application/javascript"
inline

Definition at line 67 of file types.h.

Referenced by anonymous_namespace{webapplication.cpp}::getCachingInterval().

◆ CONTENT_TYPE_JSON

const char Http::CONTENT_TYPE_JSON[] = "application/json"
inline

Definition at line 68 of file types.h.

Referenced by WebApplication::doProcessRequest().

◆ CONTENT_TYPE_PNG

const char Http::CONTENT_TYPE_PNG[] = "image/png"
inline

Definition at line 70 of file types.h.

Referenced by compressContent().

◆ CONTENT_TYPE_TXT

const char Http::CONTENT_TYPE_TXT[] = "text/plain; charset=UTF-8"
inline

◆ CRLF

const char Http::CRLF[] = {0x0D, 0x0A, '\0'}
inline

◆ HEADER_CACHE_CONTROL

const char Http::HEADER_CACHE_CONTROL[] = "cache-control"
inline

Definition at line 41 of file types.h.

Referenced by WebApplication::sendFile().

◆ HEADER_CONNECTION

const char Http::HEADER_CONNECTION[] = "connection"
inline

Definition at line 42 of file types.h.

Referenced by Http::Connection::read().

◆ HEADER_CONTENT_DISPOSITION

const char Http::HEADER_CONTENT_DISPOSITION[] = "content-disposition"
inline

Definition at line 43 of file types.h.

Referenced by Http::RequestParser::parseFormData().

◆ HEADER_CONTENT_ENCODING

const char Http::HEADER_CONTENT_ENCODING[] = "content-encoding"
inline

Definition at line 44 of file types.h.

Referenced by compressContent(), and Http::Connection::read().

◆ HEADER_CONTENT_LENGTH

const char Http::HEADER_CONTENT_LENGTH[] = "content-length"
inline

Definition at line 45 of file types.h.

Referenced by Http::RequestParser::doParse(), and toByteArray().

◆ HEADER_CONTENT_SECURITY_POLICY

const char Http::HEADER_CONTENT_SECURITY_POLICY[] = "content-security-policy"
inline

Definition at line 46 of file types.h.

Referenced by WebApplication::configure().

◆ HEADER_CONTENT_TYPE

const char Http::HEADER_CONTENT_TYPE[] = "content-type"
inline

◆ HEADER_DATE

const char Http::HEADER_DATE[] = "date"
inline

Definition at line 48 of file types.h.

Referenced by toByteArray().

◆ HEADER_HOST

const char Http::HEADER_HOST[] = "host"
inline

◆ HEADER_ORIGIN

const char Http::HEADER_ORIGIN[] = "origin"
inline

Definition at line 50 of file types.h.

Referenced by WebApplication::isCrossSiteRequest().

◆ HEADER_REFERER

const char Http::HEADER_REFERER[] = "referer"
inline

Definition at line 51 of file types.h.

Referenced by WebApplication::isCrossSiteRequest().

◆ HEADER_REFERRER_POLICY

const char Http::HEADER_REFERRER_POLICY[] = "referrer-policy"
inline

Definition at line 52 of file types.h.

Referenced by WebApplication::configure().

◆ HEADER_REQUEST_METHOD_GET

const char Http::HEADER_REQUEST_METHOD_GET[] = "GET"
inline

Definition at line 60 of file types.h.

Referenced by Http::RequestParser::doParse(), and BitTorrent::Tracker::processRequest().

◆ HEADER_REQUEST_METHOD_HEAD

const char Http::HEADER_REQUEST_METHOD_HEAD[] = "HEAD"
inline

Definition at line 61 of file types.h.

Referenced by Http::RequestParser::doParse().

◆ HEADER_REQUEST_METHOD_POST

const char Http::HEADER_REQUEST_METHOD_POST[] = "POST"
inline

Definition at line 62 of file types.h.

Referenced by Http::RequestParser::doParse().

◆ HEADER_SET_COOKIE

const char Http::HEADER_SET_COOKIE[] = "set-cookie"
inline

Definition at line 53 of file types.h.

Referenced by WebApplication::sessionEnd(), and WebApplication::sessionStart().

◆ HEADER_X_CONTENT_TYPE_OPTIONS

const char Http::HEADER_X_CONTENT_TYPE_OPTIONS[] = "x-content-type-options"
inline

Definition at line 54 of file types.h.

Referenced by WebApplication::configure().

◆ HEADER_X_FORWARDED_FOR

const char Http::HEADER_X_FORWARDED_FOR[] = "x-forwarded-for"
inline

Definition at line 55 of file types.h.

Referenced by WebApplication::resolveClientAddress().

◆ HEADER_X_FORWARDED_HOST

const char Http::HEADER_X_FORWARDED_HOST[] = "x-forwarded-host"
inline

Definition at line 56 of file types.h.

Referenced by WebApplication::isCrossSiteRequest().

◆ HEADER_X_FRAME_OPTIONS

const char Http::HEADER_X_FRAME_OPTIONS[] = "x-frame-options"
inline

Definition at line 57 of file types.h.

Referenced by WebApplication::configure().

◆ HEADER_X_XSS_PROTECTION

const char Http::HEADER_X_XSS_PROTECTION[] = "x-xss-protection"
inline

Definition at line 58 of file types.h.

Referenced by WebApplication::configure().

◆ METHOD_GET

const char Http::METHOD_GET[] = "GET"
inline

Definition at line 38 of file types.h.

Referenced by WebApplication::processRequest().

◆ METHOD_POST

const char Http::METHOD_POST[] = "POST"
inline

Definition at line 39 of file types.h.