SDRAngel
4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
|
#include <httpresponse.h>
Public Member Functions | |
HttpResponse (QTcpSocket *socket) | |
void | setHeader (QByteArray name, QByteArray value) |
void | setHeader (QByteArray name, int value) |
QMap< QByteArray, QByteArray > & | getHeaders () |
QMap< QByteArray, HttpCookie > & | getCookies () |
void | setStatus (int statusCode, QByteArray description=QByteArray()) |
int | getStatusCode () const |
void | write (QByteArray data, bool lastPart=false) |
bool | hasSentLastPart () const |
void | setCookie (const HttpCookie &cookie) |
void | redirect (const QByteArray &url) |
void | flush () |
bool | isConnected () const |
Private Member Functions | |
bool | writeToSocket (QByteArray data) |
void | writeHeaders () |
Private Attributes | |
QMap< QByteArray, QByteArray > | headers |
QTcpSocket * | socket |
int | statusCode |
QByteArray | statusText |
bool | sentHeaders |
bool | sentLastPart |
bool | chunkedMode |
QMap< QByteArray, HttpCookie > | cookies |
This object represents a HTTP response, used to return something to the web client.
response.setStatus(200,"OK"); // optional, because this is the default response.writeBody("Hello"); response.writeBody("World!",true);
Example how to return an error:
response.setStatus(500,"server error"); response.write("The request cannot be processed because the servers is broken",true);
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.
Definition at line 38 of file httpresponse.h.
HttpResponse::HttpResponse | ( | QTcpSocket * | socket | ) |
Constructor.
socket | used to write the response |
Definition at line 10 of file httpresponse.cpp.
References chunkedMode, sentHeaders, sentLastPart, socket, statusCode, and statusText.
void HttpResponse::flush | ( | ) |
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.
Definition at line 191 of file httpresponse.cpp.
References socket.
QMap< QByteArray, HttpCookie > & HttpResponse::getCookies | ( | ) |
Get the map of cookies
Definition at line 177 of file httpresponse.cpp.
References cookies.
Referenced by qtwebapp::HttpSessionStore::getSessionId().
QMap< QByteArray, QByteArray > & HttpResponse::getHeaders | ( | ) |
Get the map of HTTP response headers
Definition at line 32 of file httpresponse.cpp.
References headers.
int HttpResponse::getStatusCode | ( | ) | const |
bool HttpResponse::hasSentLastPart | ( | ) | const |
Indicates whether the body has been sent completely (write() has been called with lastPart=true).
Definition at line 161 of file httpresponse.cpp.
References sentLastPart.
bool HttpResponse::isConnected | ( | ) | const |
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.
Definition at line 197 of file httpresponse.cpp.
References socket.
void HttpResponse::redirect | ( | const QByteArray & | url | ) |
Send a redirect response to the browser. Cannot be combined with write().
url | Destination URL |
Definition at line 183 of file httpresponse.cpp.
References setHeader(), setStatus(), and write().
void HttpResponse::setCookie | ( | const HttpCookie & | cookie | ) |
Set a cookie. You must call this method before the first write().
Definition at line 167 of file httpresponse.cpp.
References cookies, qtwebapp::HttpCookie::getName(), and sentHeaders.
Referenced by qtwebapp::HttpSessionStore::getSession().
void HttpResponse::setHeader | ( | QByteArray | name, |
QByteArray | value | ||
) |
Set a HTTP response header. You must call this method before the first write().
name | name of the header |
value | value of the header |
Definition at line 20 of file httpresponse.cpp.
References headers, and sentHeaders.
Referenced by WebAPIRequestMapper::devicesetChannelIndexService(), WebAPIRequestMapper::devicesetChannelReportService(), WebAPIRequestMapper::devicesetChannelService(), WebAPIRequestMapper::devicesetChannelSettingsService(), WebAPIRequestMapper::devicesetChannelsReportService(), WebAPIRequestMapper::devicesetDeviceReportService(), WebAPIRequestMapper::devicesetDeviceRunService(), WebAPIRequestMapper::devicesetDeviceService(), WebAPIRequestMapper::devicesetDeviceSettingsService(), WebAPIRequestMapper::devicesetFocusService(), WebAPIRequestMapper::devicesetService(), WebAPIRequestMapper::instanceAMBEDevicesService(), WebAPIRequestMapper::instanceAMBESerialService(), WebAPIRequestMapper::instanceAudioInputCleanupService(), WebAPIRequestMapper::instanceAudioInputParametersService(), WebAPIRequestMapper::instanceAudioOutputCleanupService(), WebAPIRequestMapper::instanceAudioOutputParametersService(), WebAPIRequestMapper::instanceAudioService(), WebAPIRequestMapper::instanceChannelsService(), WebAPIRequestMapper::instanceConfigService(), WebAPIRequestMapper::instanceDeviceSetService(), WebAPIRequestMapper::instanceDeviceSetsService(), WebAPIRequestMapper::instanceDevicesService(), WebAPIRequestMapper::instanceLocationService(), WebAPIRequestMapper::instanceLoggingService(), WebAPIRequestMapper::instancePresetFileService(), WebAPIRequestMapper::instancePresetService(), WebAPIRequestMapper::instancePresetsService(), WebAPIRequestMapper::instanceSummaryService(), redirect(), WebAPIRequestMapper::service(), qtwebapp::StaticFileController::service(), and qtwebapp::StaticFileController::setContentType().
void HttpResponse::setHeader | ( | QByteArray | name, |
int | value | ||
) |
Set a HTTP response header. You must call this method before the first write().
name | name of the header |
value | value of the header |
Definition at line 26 of file httpresponse.cpp.
References headers, and sentHeaders.
void HttpResponse::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().
Definition at line 37 of file httpresponse.cpp.
References statusCode, and statusText.
Referenced by WebAPIRequestMapper::devicesetChannelIndexService(), WebAPIRequestMapper::devicesetChannelReportService(), WebAPIRequestMapper::devicesetChannelService(), WebAPIRequestMapper::devicesetChannelSettingsService(), WebAPIRequestMapper::devicesetChannelsReportService(), WebAPIRequestMapper::devicesetDeviceReportService(), WebAPIRequestMapper::devicesetDeviceRunService(), WebAPIRequestMapper::devicesetDeviceService(), WebAPIRequestMapper::devicesetDeviceSettingsService(), WebAPIRequestMapper::devicesetFocusService(), WebAPIRequestMapper::devicesetService(), WebAPIRequestMapper::instanceAMBEDevicesService(), WebAPIRequestMapper::instanceAMBESerialService(), WebAPIRequestMapper::instanceAudioInputCleanupService(), WebAPIRequestMapper::instanceAudioInputParametersService(), WebAPIRequestMapper::instanceAudioOutputCleanupService(), WebAPIRequestMapper::instanceAudioOutputParametersService(), WebAPIRequestMapper::instanceAudioService(), WebAPIRequestMapper::instanceChannelsService(), WebAPIRequestMapper::instanceConfigService(), WebAPIRequestMapper::instanceDeviceSetService(), WebAPIRequestMapper::instanceDeviceSetsService(), WebAPIRequestMapper::instanceDevicesService(), WebAPIRequestMapper::instanceLocationService(), WebAPIRequestMapper::instanceLoggingService(), WebAPIRequestMapper::instancePresetFileService(), WebAPIRequestMapper::instancePresetService(), WebAPIRequestMapper::instancePresetsService(), WebAPIRequestMapper::instanceSummaryService(), WebAPIRequestMapper::parseJsonBody(), redirect(), WebAPIRequestMapper::service(), qtwebapp::HttpRequestHandler::service(), and qtwebapp::StaticFileController::service().
void HttpResponse::write | ( | QByteArray | data, |
bool | lastPart = false |
||
) |
Write body data to the socket.
The HTTP status line, headers and cookies are sent automatically before the body.
If the response contains only a single chunk (indicated by lastPart=true), then a Content-Length header is automatically set.
Chunked mode is automatically selected if there is no Content-Length header and also no Connection:close header.
data | Data bytes of the body |
lastPart | Indicates that this is the last chunk of data and flushes the output buffer. |
Definition at line 98 of file httpresponse.cpp.
References chunkedMode, headers, sentHeaders, sentLastPart, socket, writeHeaders(), and writeToSocket().
Referenced by WebAPIRequestMapper::devicesetChannelIndexService(), WebAPIRequestMapper::devicesetChannelReportService(), WebAPIRequestMapper::devicesetChannelService(), WebAPIRequestMapper::devicesetChannelSettingsService(), WebAPIRequestMapper::devicesetChannelsReportService(), WebAPIRequestMapper::devicesetDeviceReportService(), WebAPIRequestMapper::devicesetDeviceRunService(), WebAPIRequestMapper::devicesetDeviceService(), WebAPIRequestMapper::devicesetDeviceSettingsService(), WebAPIRequestMapper::devicesetFocusService(), WebAPIRequestMapper::devicesetService(), WebAPIRequestMapper::instanceAMBEDevicesService(), WebAPIRequestMapper::instanceAMBESerialService(), WebAPIRequestMapper::instanceAudioInputCleanupService(), WebAPIRequestMapper::instanceAudioInputParametersService(), WebAPIRequestMapper::instanceAudioOutputCleanupService(), WebAPIRequestMapper::instanceAudioOutputParametersService(), WebAPIRequestMapper::instanceAudioService(), WebAPIRequestMapper::instanceChannelsService(), WebAPIRequestMapper::instanceConfigService(), WebAPIRequestMapper::instanceDeviceSetService(), WebAPIRequestMapper::instanceDeviceSetsService(), WebAPIRequestMapper::instanceDevicesService(), WebAPIRequestMapper::instanceLocationService(), WebAPIRequestMapper::instanceLoggingService(), WebAPIRequestMapper::instancePresetFileService(), WebAPIRequestMapper::instancePresetService(), WebAPIRequestMapper::instancePresetsService(), WebAPIRequestMapper::instanceSummaryService(), WebAPIRequestMapper::parseJsonBody(), redirect(), WebAPIRequestMapper::service(), qtwebapp::HttpRequestHandler::service(), and qtwebapp::StaticFileController::service().
|
private |
Write the response HTTP status and headers to the socket. Calling this method is optional, because writeBody() calls it automatically when required.
Definition at line 48 of file httpresponse.cpp.
References cookies, headers, sentHeaders, statusCode, statusText, qtwebapp::HttpCookie::toByteArray(), and writeToSocket().
Referenced by write().
|
private |
Write raw data to the socket. This method blocks until all bytes have been passed to the TCP buffer
Definition at line 75 of file httpresponse.cpp.
References socket.
Referenced by write(), and writeHeaders().
|
private |
Whether the response is sent in chunked mode
Definition at line 146 of file httpresponse.h.
Referenced by HttpResponse(), and write().
|
private |
Cookies
Definition at line 149 of file httpresponse.h.
Referenced by getCookies(), setCookie(), and writeHeaders().
|
private |
Request headers
Definition at line 128 of file httpresponse.h.
Referenced by getHeaders(), setHeader(), write(), and writeHeaders().
|
private |
Indicator whether headers have been sent
Definition at line 140 of file httpresponse.h.
Referenced by HttpResponse(), setCookie(), setHeader(), write(), and writeHeaders().
|
private |
Indicator whether the body has been sent completely
Definition at line 143 of file httpresponse.h.
Referenced by hasSentLastPart(), HttpResponse(), and write().
|
private |
Socket for writing output
Definition at line 131 of file httpresponse.h.
Referenced by flush(), HttpResponse(), isConnected(), write(), and writeToSocket().
|
private |
HTTP status code
Definition at line 134 of file httpresponse.h.
Referenced by getStatusCode(), HttpResponse(), setStatus(), and writeHeaders().
|
private |
HTTP status code description
Definition at line 137 of file httpresponse.h.
Referenced by HttpResponse(), setStatus(), and writeHeaders().