Hobrasoft httpd server
Embedded HTTP server for Qt and C++
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | List of all members
HobrasoftHttpd::HttpRequest Class Reference

Processes HTTP request, parses headers, body and files sent by HTTP protocol. More...

#include <httprequest.h>

Public Types

enum  Status {
  StatusWaitForRequest, StatusWaitForHeader, StatusWaitForBody, StatusComplete,
  StatusAbort
}
 Inner state of request, changes when parts of request are read from networkd. More...
 

Public Member Functions

 HttpRequest (HttpConnection *connection)
 Constructor sets default falues from configuration.
 
HttpConnectionconnection () const
 Returns HttpConnection of the request.
 
QHostAddress peerAddress () const
 Returns peer's host address.
 
void readFromSocket (QTcpSocket *socket)
 Reads data from socket. More...
 
Status status () const
 Returns current status of the request.
 
QString statusString () const
 Returns current status of the request as a string.
 
QString method () const
 Returns used HTTP method of the request (GET, PUT, POST or DELETE)
 
QString path () const
 Returns path of the request (/files/index.html)
 
QString fullPath () const
 Returns full path of the request (/files/index.html) including parameters.
 
QString version () const
 Returns version of HTTP protocol (HTTP/1.1)
 
QString header (const QString &name) const
 Returns requested header value, case insensitive.
 
QList< QString > headers (const QString &name) const
 Returns all headers of HTTP request in QList, case insensitive. More...
 
QMultiMap< QString, QString > headerMap () const
 Returns all headers of HTTP request in QMap. More...
 
QString parameter (const QString &name) const
 Returns parameter of the HTTP request. More...
 
QList< QString > parameters (const QString &name) const
 Returns all parameters of the HTTP request in QList.
 
QMultiMap< QString, QString > parameterMap () const
 Returns all parameters of the HTTP request in QMap.
 
QByteArray body () const
 Returns the body of the request. More...
 
QString cookie (const QString &name) const
 Returns cookie.
 
const QMap< QString, QString > & cookieMap ()
 Returns all cookies of the request in QMap.
 
QTemporaryFile * uploadedFile (const QString &fieldName)
 Returns temporary file with uploaded file from html form. More...
 
const QMap< QString, QTemporaryFile * > & uploadedFiles () const
 Returns list of uploaded files. More...
 
const QMap< QString, QString > & contentTypes () const
 Returns list of content types of uploaded files. More...
 
QString contentType (const QString &fieldName) const
 Returns content type of uploaded file.
 
const QDateTime & datetime () const
 Returns date and time of the request.
 

Static Public Member Functions

static QString urlDecode (const QString &source)
 Converts URL encoded string to UTF8 string. More...
 

Private Member Functions

void parseMultiPartFile ()
 Parses the mime multipart request.
 
void readRequest (QTcpSocket *socket)
 Reads request from socket. More...
 
void readHeader (QTcpSocket *socket)
 Reads HTTP headers of the request.
 
void readBody (QTcpSocket *socket)
 Reads body of the request.
 
void decodeRequestParams ()
 Parses parameters of the URL.
 
void extractCookies ()
 Parses cookies.
 

Detailed Description

Processes HTTP request, parses headers, body and files sent by HTTP protocol.

Definition at line 23 of file httprequest.h.

Member Enumeration Documentation

◆ Status

Inner state of request, changes when parts of request are read from networkd.

Enumerator
StatusWaitForRequest 

Waits for request.

StatusWaitForHeader 

Waits for header.

StatusWaitForBody 

Waits for body.

StatusComplete 

Request is complete.

StatusAbort 

Request is canceled.

Definition at line 28 of file httprequest.h.

Member Function Documentation

◆ body()

QByteArray HobrasoftHttpd::HttpRequest::body ( ) const
inline

Returns the body of the request.

Use this method when you need to read POST requests

Definition at line 133 of file httprequest.h.

◆ contentTypes()

const QMap<QString, QString>& HobrasoftHttpd::HttpRequest::contentTypes ( ) const
inline

Returns list of content types of uploaded files.

Returned map:

  • Key: name of the fiels
  • Value: content type

Definition at line 178 of file httprequest.h.

◆ headerMap()

QMultiMap<QString, QString> HobrasoftHttpd::HttpRequest::headerMap ( ) const
inline

Returns all headers of HTTP request in QMap.

Cookies are not contined in the returnetd values. If you want to read cookies, use cookie() method instead.

Definition at line 107 of file httprequest.h.

◆ headers()

QList< QString > HttpRequest::headers ( const QString &  name) const

Returns all headers of HTTP request in QList, case insensitive.

Cookies are not contined in the returnetd values. If you want to read cookies, use cookie() method instead.

Definition at line 378 of file httprequest.cpp.

◆ parameter()

QString HobrasoftHttpd::HttpRequest::parameter ( const QString &  name) const
inline

Returns parameter of the HTTP request.

Parameters are typically used in GET requests:

http://my-own-server.com/path?param1=abc&param2=xyz

Definition at line 116 of file httprequest.h.

◆ readFromSocket()

void HttpRequest::readFromSocket ( QTcpSocket *  socket)

Reads data from socket.

Called from HttpConnection when reading the request

Definition at line 28 of file httprequest.cpp.

◆ readRequest()

void HttpRequest::readRequest ( QTcpSocket *  socket)
private

Reads request from socket.

Parses first line of the request and stores the method (GET, PUT, POST...) the path (/path-to-request), version (HTTP/1.1) to class instance.

Definition at line 62 of file httprequest.cpp.

◆ uploadedFile()

QTemporaryFile* HobrasoftHttpd::HttpRequest::uploadedFile ( const QString &  fieldName)
inline

Returns temporary file with uploaded file from html form.

If the returned file is closed, then the file is deleted. If you want to reuse the file, do not close it.

Definition at line 156 of file httprequest.h.

◆ uploadedFiles()

const QMap<QString, QTemporaryFile *>& HobrasoftHttpd::HttpRequest::uploadedFiles ( ) const
inline

Returns list of uploaded files.

Returned map:

  • Key: name of the field
  • Value: opened temporary file

If the returned file is closed, then the file is deleted. If you want to reuse the file, do not close it.

Definition at line 168 of file httprequest.h.

◆ urlDecode()

QString HttpRequest::urlDecode ( const QString &  source)
static

Converts URL encoded string to UTF8 string.

Dedodes % encoding used in URL.

Definition at line 259 of file httprequest.cpp.


The documentation for this class was generated from the following files: