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.
Public Member Functions | Private Member Functions | Private Attributes | List of all members
qtwebapp::HttpResponse Class Reference

#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, HttpCookiecookies
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ HttpResponse()

HttpResponse::HttpResponse ( QTcpSocket *  socket)

Constructor.

Parameters
socketused to write the response

Definition at line 10 of file httpresponse.cpp.

References chunkedMode, sentHeaders, sentLastPart, socket, statusCode, and statusText.

11 {
12  this->socket=socket;
13  statusCode=200;
14  statusText="OK";
15  sentHeaders=false;
16  sentLastPart=false;
17  chunkedMode=false;
18 }

Member Function Documentation

◆ flush()

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.

192 {
193  socket->flush();
194 }

◆ getCookies()

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().

178 {
179  return cookies;
180 }
QMap< QByteArray, HttpCookie > cookies
Definition: httpresponse.h:149
+ Here is the caller graph for this function:

◆ getHeaders()

QMap< QByteArray, QByteArray > & HttpResponse::getHeaders ( )

Get the map of HTTP response headers

Definition at line 32 of file httpresponse.cpp.

References headers.

33 {
34  return headers;
35 }
QMap< QByteArray, QByteArray > headers
Definition: httpresponse.h:128

◆ getStatusCode()

int HttpResponse::getStatusCode ( ) const

Return the status code.

Definition at line 43 of file httpresponse.cpp.

References statusCode.

44 {
45  return this->statusCode;
46 }

◆ hasSentLastPart()

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.

162 {
163  return sentLastPart;
164 }

◆ isConnected()

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.

198 {
199  return socket->isOpen();
200 }

◆ redirect()

void HttpResponse::redirect ( const QByteArray &  url)

Send a redirect response to the browser. Cannot be combined with write().

Parameters
urlDestination URL

Definition at line 183 of file httpresponse.cpp.

References setHeader(), setStatus(), and write().

184 {
185  setStatus(303,"See Other");
186  setHeader("Location",url);
187  write("Redirect",true);
188 }
void setHeader(QByteArray name, QByteArray value)
void write(QByteArray data, bool lastPart=false)
void setStatus(int statusCode, QByteArray description=QByteArray())
+ Here is the call graph for this function:

◆ setCookie()

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().

168 {
169  Q_ASSERT(sentHeaders==false);
170  if (!cookie.getName().isEmpty())
171  {
172  cookies.insert(cookie.getName(),cookie);
173  }
174 }
QByteArray getName() const
Definition: httpcookie.cpp:174
QMap< QByteArray, HttpCookie > cookies
Definition: httpresponse.h:149
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setHeader() [1/2]

void HttpResponse::setHeader ( QByteArray  name,
QByteArray  value 
)

Set a HTTP response header. You must call this method before the first write().

Parameters
namename of the header
valuevalue 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().

21 {
22  Q_ASSERT(sentHeaders==false);
23  headers.insert(name,value);
24 }
QMap< QByteArray, QByteArray > headers
Definition: httpresponse.h:128
+ Here is the caller graph for this function:

◆ setHeader() [2/2]

void HttpResponse::setHeader ( QByteArray  name,
int  value 
)

Set a HTTP response header. You must call this method before the first write().

Parameters
namename of the header
valuevalue of the header

Definition at line 26 of file httpresponse.cpp.

References headers, and sentHeaders.

27 {
28  Q_ASSERT(sentHeaders==false);
29  headers.insert(name,QByteArray::number(value));
30 }
QMap< QByteArray, QByteArray > headers
Definition: httpresponse.h:128

◆ setStatus()

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().

38 {
39  this->statusCode=statusCode;
40  statusText=description;
41 }
+ Here is the caller graph for this function:

◆ write()

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.

Parameters
dataData bytes of the body
lastPartIndicates 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().

99 {
100  Q_ASSERT(sentLastPart==false);
101 
102  // Send HTTP headers, if not already done (that happens only on the first call to write())
103  if (sentHeaders==false)
104  {
105  // If the whole response is generated with a single call to write(), then we know the total
106  // size of the response and therefore can set the Content-Length header automatically.
107  if (lastPart)
108  {
109  // Automatically set the Content-Length header
110  headers.insert("Content-Length",QByteArray::number(data.size()));
111  }
112 
113  // else if we will not close the connection at the end, them we must use the chunked mode.
114  else
115  {
116  QByteArray connectionValue=headers.value("Connection",headers.value("connection"));
117  bool connectionClose=QString::compare(connectionValue,"close",Qt::CaseInsensitive)==0;
118  if (!connectionClose)
119  {
120  headers.insert("Transfer-Encoding","chunked");
121  chunkedMode=true;
122  }
123  }
124 
125  writeHeaders();
126  }
127 
128  // Send data
129  if (data.size()>0)
130  {
131  if (chunkedMode)
132  {
133  if (data.size()>0)
134  {
135  QByteArray size=QByteArray::number(data.size(),16);
136  writeToSocket(size);
137  writeToSocket("\r\n");
138  writeToSocket(data);
139  writeToSocket("\r\n");
140  }
141  }
142  else
143  {
144  writeToSocket(data);
145  }
146  }
147 
148  // Only for the last chunk, send the terminating marker and flush the buffer.
149  if (lastPart)
150  {
151  if (chunkedMode)
152  {
153  writeToSocket("0\r\n\r\n");
154  }
155  socket->flush();
156  sentLastPart=true;
157  }
158 }
QMap< QByteArray, QByteArray > headers
Definition: httpresponse.h:128
bool writeToSocket(QByteArray data)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ writeHeaders()

void HttpResponse::writeHeaders ( )
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().

49 {
50  Q_ASSERT(sentHeaders==false);
51  QByteArray buffer;
52  buffer.append("HTTP/1.1 ");
53  buffer.append(QByteArray::number(statusCode));
54  buffer.append(' ');
55  buffer.append(statusText);
56  buffer.append("\r\n");
57  foreach(QByteArray name, headers.keys())
58  {
59  buffer.append(name);
60  buffer.append(": ");
61  buffer.append(headers.value(name));
62  buffer.append("\r\n");
63  }
64  foreach(HttpCookie cookie,cookies.values())
65  {
66  buffer.append("Set-Cookie: ");
67  buffer.append(cookie.toByteArray());
68  buffer.append("\r\n");
69  }
70  buffer.append("\r\n");
71  writeToSocket(buffer);
72  sentHeaders=true;
73 }
QMap< QByteArray, QByteArray > headers
Definition: httpresponse.h:128
QByteArray toByteArray() const
Definition: httpcookie.cpp:98
QMap< QByteArray, HttpCookie > cookies
Definition: httpresponse.h:149
bool writeToSocket(QByteArray data)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ writeToSocket()

bool HttpResponse::writeToSocket ( QByteArray  data)
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().

76 {
77  int remaining=data.size();
78  char* ptr=data.data();
79  while (socket->isOpen() && remaining>0)
80  {
81  // If the output buffer has become large, then wait until it has been sent.
82  if (socket->bytesToWrite()>16384)
83  {
84  socket->waitForBytesWritten(-1);
85  }
86 
87  int written=socket->write(ptr,remaining);
88  if (written==-1)
89  {
90  return false;
91  }
92  ptr+=written;
93  remaining-=written;
94  }
95  return true;
96 }
+ Here is the caller graph for this function:

Member Data Documentation

◆ chunkedMode

bool qtwebapp::HttpResponse::chunkedMode
private

Whether the response is sent in chunked mode

Definition at line 146 of file httpresponse.h.

Referenced by HttpResponse(), and write().

◆ cookies

QMap<QByteArray,HttpCookie> qtwebapp::HttpResponse::cookies
private

Cookies

Definition at line 149 of file httpresponse.h.

Referenced by getCookies(), setCookie(), and writeHeaders().

◆ headers

QMap<QByteArray,QByteArray> qtwebapp::HttpResponse::headers
private

Request headers

Definition at line 128 of file httpresponse.h.

Referenced by getHeaders(), setHeader(), write(), and writeHeaders().

◆ sentHeaders

bool qtwebapp::HttpResponse::sentHeaders
private

Indicator whether headers have been sent

Definition at line 140 of file httpresponse.h.

Referenced by HttpResponse(), setCookie(), setHeader(), write(), and writeHeaders().

◆ sentLastPart

bool qtwebapp::HttpResponse::sentLastPart
private

Indicator whether the body has been sent completely

Definition at line 143 of file httpresponse.h.

Referenced by hasSentLastPart(), HttpResponse(), and write().

◆ socket

QTcpSocket* qtwebapp::HttpResponse::socket
private

Socket for writing output

Definition at line 131 of file httpresponse.h.

Referenced by flush(), HttpResponse(), isConnected(), write(), and writeToSocket().

◆ statusCode

int qtwebapp::HttpResponse::statusCode
private

HTTP status code

Definition at line 134 of file httpresponse.h.

Referenced by getStatusCode(), HttpResponse(), setStatus(), and writeHeaders().

◆ statusText

QByteArray qtwebapp::HttpResponse::statusText
private

HTTP status code description

Definition at line 137 of file httpresponse.h.

Referenced by HttpResponse(), setStatus(), and writeHeaders().


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