-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathStreamHttpReply.cpp
More file actions
173 lines (134 loc) · 5.29 KB
/
StreamHttpReply.cpp
File metadata and controls
173 lines (134 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "StreamHttpReply.hpp"
#include <Arduino.h>
#include "ArduinoHttpServerDebug.h"
//------------------------------------------------------------------------------
// Class Definition
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//! \brief Constructor.
ArduinoHttpServer::AbstractStreamHttpReply::AbstractStreamHttpReply(Stream& stream, const String& contentType, const String& code) :
m_stream(stream),
m_contentType(contentType),
m_code(code)
{
}
void ArduinoHttpServer::AbstractStreamHttpReply::addHeader(const String& name, const String &value) {
if (m_headerCount == MAX_HEADERS) return;
m_headerNames[m_headerCount] = name;
m_headerValues[m_headerCount] = value;
m_headerCount++;
}
//------------------------------------------------------------------------------
//! \brief Send this reply / print this reply to stream.
//! \todo: Accept char* also for data coming directly from flash.
void ArduinoHttpServer::AbstractStreamHttpReply::send(const String& data, const String& title)
{
// Read away remaining bytes.
while(getStream().read()>=0);
DEBUG_ARDUINO_HTTP_SERVER_PRINT("Printing Reply ... ");
getStream().print( AHS_F("HTTP/1.1 ") );
getStream().print( getCode() + " ");
getStream().print( title + "\r\n" );
getStream().print( AHS_F("Connection: close\r\n") );
getStream().print( AHS_F("Content-Length: ") ); getStream().print( data.length()); getStream().print( AHS_F("\r\n") );
getStream().print( AHS_F("Content-Type: ") ); getStream().print( m_contentType ); getStream().print( AHS_F("\r\n") );
for(int i = 0; i < m_headerCount; i++) {
getStream().print( m_headerNames[i] ); getStream().print(": "); getStream().print( m_headerValues[i] ); getStream().print( AHS_F("\r\n") );
}
getStream().print( AHS_F("\r\n") );
getStream().print( data ); getStream().print( AHS_F("\r\n") );
DEBUG_ARDUINO_HTTP_SERVER_PRINTLN("done.");
}
Stream& ArduinoHttpServer::AbstractStreamHttpReply::getStream()
{
return m_stream;
}
const String& ArduinoHttpServer::AbstractStreamHttpReply::getCode()
{
return m_code;
}
const String& ArduinoHttpServer::AbstractStreamHttpReply::getContentType()
{
if(m_contentType.length()<=0)
{
m_contentType = CONTENT_TYPE_TEXT_HTML; // Default content type.
}
return m_contentType;
}
//------------------------------------------------------------------------------
// Class Definition
//------------------------------------------------------------------------------
ArduinoHttpServer::StreamHttpReply::StreamHttpReply(Stream& stream, const String& contentType) :
AbstractStreamHttpReply(stream, contentType, "200")
{
}
//------------------------------------------------------------------------------
// Class Definition
//------------------------------------------------------------------------------
ArduinoHttpServer::StreamHttpErrorReply::StreamHttpErrorReply(Stream& stream, const String& contentType, const String& code) :
AbstractStreamHttpReply(stream, contentType, code)
{
}
void ArduinoHttpServer::StreamHttpErrorReply::send(const String& data)
{
String body;
if(getContentType() == CONTENT_TYPE_TEXT_HTML)
{
body = getHtmlBody(data);
}
else if(getContentType() == CONTENT_TYPE_APPLICATION_JSON)
{
body = getJsonBody(data);
}
else
{
body = data;
}
AbstractStreamHttpReply::send(body, data);
}
String ArduinoHttpServer::StreamHttpErrorReply::getHtmlBody(const String& data)
{
String body;
body += AHS_F("<html><head><title>Error: ");
body += getCode();
body += AHS_F("</title></head><body><h3>Error ");
body += getCode();
body += AHS_F(": ");
body += data;
body += AHS_F("</h3></body></html>");
return body;
}
String ArduinoHttpServer::StreamHttpErrorReply::getJsonBody(const String& data)
{
// Copy string since replace modifies original.
String dataCopy(data);
dataCopy.replace("\"", "\\\"");
String body;
body += AHS_F("{\"Error\": \"");
body +=dataCopy;
body += AHS_F("\"}");
return body;
}
//------------------------------------------------------------------------------
// Class Definition
//------------------------------------------------------------------------------
#ifndef ARDUINO_HTTP_SERVER_NO_BASIC_AUTH
ArduinoHttpServer::StreamHttpAuthenticateReply::StreamHttpAuthenticateReply(Stream& stream, const String& contentType) :
AbstractStreamHttpReply(stream, contentType, "401")
{
}
void ArduinoHttpServer::StreamHttpAuthenticateReply::send()
{
// Read away remaining bytes.
while(getStream().read()>=0);
DEBUG_ARDUINO_HTTP_SERVER_PRINT("Printing authenticate reply ... ");
getStream().println(AHS_F("HTTP/1.1 401 Unauthorized"));
getStream().println(AHS_F("WWW-Authenticate: Basic realm=\"Login Required\""));
getStream().println(AHS_F("Connection: close"));
getStream().println(AHS_F(""));
getStream().println(AHS_F("<html><head><title>401 Unauthorized</title></head><body><h4>401 Unauthorized</h4>Authorization required.</body></html>"));
getStream().println(AHS_F(""));
getStream().println(AHS_F(""));
DEBUG_ARDUINO_HTTP_SERVER_PRINTLN("done.");
}
#endif