QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 "qgslogger.h"
22 #include "qgsmessagelog.h"
23 
24 #include <QDebug>
25 
26 //
27 // QgsBufferServerResponse
28 //
29 
31 {
32  mBuffer.open( QIODevice::ReadWrite );
33 }
34 
35 void QgsBufferServerResponse::removeHeader( const QString &key )
36 {
37  if ( !mHeadersSent )
38  mHeaders.remove( key );
39 }
40 
41 void QgsBufferServerResponse::setHeader( const QString &key, const QString &value )
42 {
43  if ( ! mHeadersSent )
44  mHeaders.insert( key, value );
45 }
46 
48 {
49  mStatusCode = code;
50 }
51 
52 QString QgsBufferServerResponse::header( const QString &key ) const
53 {
54  return mHeaders.value( key );
55 }
56 
58 {
59  return mHeadersSent;
60 }
61 
62 void QgsBufferServerResponse::sendError( int code, const QString &message )
63 {
64  if ( mHeadersSent )
65  {
66  QgsMessageLog::logMessage( "Cannot send error after headers sent" );
67  return;
68  }
69 
70  clear();
71  setStatusCode( code );
72  setHeader( QStringLiteral( "Content-Type" ), QStringLiteral( "text/plain; charset=utf-8" ) );
73  write( message );
74  finish();
75 }
76 
78 {
79  return &mBuffer;
80 }
81 
83 {
84  if ( mFinished )
85  {
86  QgsMessageLog::logMessage( "finish() called twice" );
87  return;
88  }
89 
90  if ( !mHeadersSent )
91  {
92  if ( ! mHeaders.contains( "Content-Length" ) )
93  {
94  mHeaders.insert( QStringLiteral( "Content-Length" ), QStringLiteral( "%1" ).arg( mBuffer.pos() ) );
95  }
96  }
97  flush();
98  mFinished = true;
99 }
100 
102 {
103  if ( ! mHeadersSent )
104  {
105  mHeadersSent = true;
106  }
107 
108  mBuffer.seek( 0 );
109  QByteArray &ba = mBuffer.buffer();
110  mBody.append( ba );
111  ba.clear();
112 }
113 
114 
116 {
117  mHeaders.clear();
118  mBuffer.seek( 0 );
119  mBuffer.buffer().clear();
120 }
121 
122 
124 {
125  return mBuffer.data();
126 }
127 
128 
130 {
131  mBuffer.seek( 0 );
132  mBuffer.buffer().clear();
133 }
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 setStatusCode(int code) override
Set the http status code.
void clear() override
Reset all headers and content for this response.
void removeHeader(const QString &key) override
Clear header Undo a previous &#39;setHeader&#39; call.
virtual void write(const QString &data)
Write string This is a convenient method that will write directly to the underlying I/O device...
void finish() override
Finish the response, ending the transaction.
void truncate() override
Truncate data.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
QByteArray data() const override
Gets the data written so far.
QString header(const QString &key) const override
Returns the header value.
void flush() override
Flushes the current output buffer to the network.
void sendError(int code, const QString &message) override
Send error This method delegates error handling at the server level.
QIODevice * io() override
Returns the underlying QIODevice.
bool headersSent() const override
Returns true if the headers have already been sent.