QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
qgsbufferserverresponse.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsbufferserverresponse.cpp
3 
4  Define response wrapper for buffer response
5  -------------------
6  begin : 2017-01-03
7  copyright : (C) 2017 by David Marteau
8  email : david dot marteau at 3liz dot com
9  ***************************************************************************/
10 
11 /***************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  ***************************************************************************/
19 
21 #include "qgsmessagelog.h"
22 
23 //
24 // QgsBufferServerResponse
25 //
26 
28 {
29  mBuffer.open( QIODevice::ReadWrite );
30 }
31 
32 void QgsBufferServerResponse::removeHeader( const QString &key )
33 {
34  if ( !mHeadersSent )
35  mHeaders.remove( key );
36 }
37 
38 void QgsBufferServerResponse::setHeader( const QString &key, const QString &value )
39 {
40  if ( ! mHeadersSent )
41  mHeaders.insert( key, value );
42 }
43 
45 {
46  mStatusCode = code;
47 }
48 
49 QString QgsBufferServerResponse::header( const QString &key ) const
50 {
51  return mHeaders.value( key );
52 }
53 
55 {
56  return mHeadersSent;
57 }
58 
59 void QgsBufferServerResponse::sendError( int code, const QString &message )
60 {
61  if ( mHeadersSent )
62  {
63  QgsMessageLog::logMessage( "Cannot send error after headers sent" );
64  return;
65  }
66 
67  clear();
68  setStatusCode( code );
69  setHeader( QStringLiteral( "Content-Type" ), QStringLiteral( "text/plain; charset=utf-8" ) );
70  write( message );
71  finish();
72 }
73 
75 {
76  return &mBuffer;
77 }
78 
80 {
81  if ( mFinished )
82  {
83  QgsMessageLog::logMessage( "finish() called twice" );
84  return;
85  }
86 
87  if ( !mHeadersSent )
88  {
89  if ( ! mHeaders.contains( "Content-Length" ) )
90  {
91  mHeaders.insert( QStringLiteral( "Content-Length" ), QString::number( mBuffer.pos() ) );
92  }
93  }
94  flush();
95  mFinished = true;
96 }
97 
99 {
100  if ( ! mHeadersSent )
101  {
102  mHeadersSent = true;
103  }
104 
105  mBuffer.seek( 0 );
106  QByteArray &ba = mBuffer.buffer();
107  mBody.append( ba );
108  ba.clear();
109 }
110 
111 
113 {
114  mHeaders.clear();
115  mBuffer.seek( 0 );
116  mBuffer.buffer().clear();
117 }
118 
119 
121 {
122  return mBuffer.data();
123 }
124 
125 
127 {
128  mBuffer.seek( 0 );
129  mBuffer.buffer().clear();
130 }
void setStatusCode(int code) override
Set the http status code.
void flush() override
Flushes the current output buffer to the network.
QString header(const QString &key) const override
Returns the header value.
QByteArray data() const override
Gets the data written so far.
void finish() override
Finish the response, ending the transaction.
void sendError(int code, const QString &message) override
Send error This method delegates error handling at the server level.
void setHeader(const QString &key, const QString &value) override
Set Header entry Add Header entry to the response Note that it is usually an error to set Header afte...
void clear() override
Reset all headers and content for this response.
bool headersSent() const override
Returns true if the headers have already been sent.
void truncate() override
Truncate data.
void removeHeader(const QString &key) override
Clear header Undo a previous 'setHeader' call.
QIODevice * io() override
Returns the underlying QIODevice.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
virtual void write(const QString &data)
Write string This is a convenient method that will write directly to the underlying I/O device.