Update httpserver to 1.6.5

This commit is contained in:
Luis Ángel San Martín
2016-06-22 19:09:04 +02:00
parent 44d7c892c1
commit 706e0921f3
22 changed files with 1491 additions and 1198 deletions

View File

@ -9,12 +9,12 @@
#include <QMap>
#include <QString>
#include <QTcpSocket>
#include "httpglobal.h"
#include "httpcookie.h"
/**
This object represents a HTTP response, in particular the response headers.
This object represents a HTTP response, used to return something to the web client.
<p>
Example code for proper response generation:
<code><pre>
response.setStatus(200,"OK"); // optional, because this is the default
response.writeBody("Hello");
@ -27,108 +27,132 @@
response.write("The request cannot be processed because the servers is broken",true);
</pre></code>
<p>
For performance reason, writing a single or few large packets is better than writing
many small packets. In case of large responses (e.g. file downloads), a Content-Length
header should be set before calling write(). Web Browsers use that information to display
a progress bar.
In case of large responses (e.g. file downloads), a Content-Length header should be set
before calling write(). Web Browsers use that information to display a progress bar.
*/
class HttpResponse {
class DECLSPEC HttpResponse {
Q_DISABLE_COPY(HttpResponse)
public:
/**
Constructor.
@param socket used to write the response
*/
HttpResponse(QTcpSocket* socket);
/**
Constructor.
@param socket used to write the response
*/
HttpResponse(QTcpSocket* socket);
/**
Set a HTTP response header
@param name name of the header
@param value value of the header
*/
void setHeader(QByteArray name, QByteArray value);
/**
Set a HTTP response header.
You must call this method before the first write().
@param name name of the header
@param value value of the header
*/
void setHeader(QByteArray name, QByteArray value);
/**
Set a HTTP response header
@param name name of the header
@param value value of the header
*/
void setHeader(QByteArray name, int value);
/**
Set a HTTP response header.
You must call this method before the first write().
@param name name of the header
@param value value of the header
*/
void setHeader(QByteArray name, int value);
/** Get the map of HTTP response headers */
QMap<QByteArray,QByteArray>& getHeaders();
/** Get the map of HTTP response headers */
QMap<QByteArray,QByteArray>& getHeaders();
/** Get the map of cookies */
QMap<QByteArray,HttpCookie>& getCookies();
/** Get the map of cookies */
QMap<QByteArray,HttpCookie>& getCookies();
/**
Set status code and description. The default is 200,OK.
*/
void setStatus(int statusCode, QByteArray description=QByteArray());
/**
Set status code and description. The default is 200,OK.
You must call this method before the first write().
*/
void setStatus(int statusCode, QByteArray description=QByteArray());
/**
Write body data to the socket.
<p>
The HTTP status line and headers are sent automatically before the first
byte of the body gets sent.
<p>
If the response contains only a single chunk (indicated by lastPart=true),
the response is transferred in traditional mode with a Content-Length
header, which is automatically added if not already set before.
<p>
Otherwise, each part is transferred in chunked mode.
@param data Data bytes of the body
@param lastPart Indicator, if this is the last part of the response.
*/
void write(QByteArray data, bool lastPart=false);
void writeText(QString text, bool lastPart=false);
/** Return the status code. */
int getStatusCode() const;
/**
Indicates wheter the body has been sent completely. Used by the connection
handler to terminate the body automatically when necessary.
*/
bool hasSentLastPart() const;
/**
Write body data to the socket.
<p>
The HTTP status line, headers and cookies are sent automatically before the body.
<p>
If the response contains only a single chunk (indicated by lastPart=true),
then a Content-Length header is automatically set.
<p>
Chunked mode is automatically selected if there is no Content-Length header
and also no Connection:close header.
@param data Data bytes of the body
@param lastPart Indicates that this is the last chunk of data and flushes the output buffer.
*/
void write(QByteArray data, bool lastPart=false);
/**
Set a cookie. Cookies are sent together with the headers when the first
call to write() occurs.
*/
void setCookie(const HttpCookie& cookie);
/**
Indicates whether the body has been sent completely (write() has been called with lastPart=true).
*/
bool hasSentLastPart() const;
/**
Set a cookie.
You must call this method before the first write().
*/
void setCookie(const HttpCookie& cookie);
/**
Send a redirect response to the browser.
Cannot be combined with write().
@param url Destination URL
*/
void redirect(const QByteArray& url);
/**
* Flush the output buffer (of the underlying socket).
* You normally don't need to call this method because flush is
* automatically called after HttpRequestHandler::service() returns.
*/
void flush();
/**
* May be used to check whether the connection to the web client has been lost.
* This might be useful to cancel the generation of large or slow responses.
*/
bool isConnected() const;
private:
/** Request headers */
QMap<QByteArray,QByteArray> headers;
/** Request headers */
QMap<QByteArray,QByteArray> headers;
/** Socket for writing output */
QTcpSocket* socket;
/** Socket for writing output */
QTcpSocket* socket;
/** HTTP status code*/
int statusCode;
/** HTTP status code*/
int statusCode;
/** HTTP status code description */
QByteArray statusText;
/** HTTP status code description */
QByteArray statusText;
/** Indicator whether headers have been sent */
bool sentHeaders;
/** Indicator whether headers have been sent */
bool sentHeaders;
/** Indicator whether the body has been sent completely */
bool sentLastPart;
/** Indicator whether the body has been sent completely */
bool sentLastPart;
/** Cookies */
QMap<QByteArray,HttpCookie> cookies;
/** Whether the response is sent in chunked mode */
bool chunkedMode;
/** Write raw data to the socket. This method blocks until all bytes have been passed to the TCP buffer */
bool writeToSocket(QByteArray data);
/** Cookies */
QMap<QByteArray,HttpCookie> cookies;
/**
Write the response HTTP status and headers to the socket.
Calling this method is optional, because writeBody() calls
it automatically when required.
*/
void writeHeaders();
/** Write raw data to the socket. This method blocks until all bytes have been passed to the TCP buffer */
bool writeToSocket(QByteArray data);
/**
Write the response HTTP status and headers to the socket.
Calling this method is optional, because writeBody() calls
it automatically when required.
*/
void writeHeaders();
};