31
31
#define DEBUG_OUTPUT Serial
32
32
#endif
33
33
34
+ bool redirectRequest (WiFiClient& client, String& methodStr, String& url, String& searchStr, String& versionStr){
35
+ String HostStr = url.substring (5 );
36
+ String URIStr = " /" ;
37
+ int hasURI = HostStr.indexOf (' /' );
38
+ if (hasURI != -1 ){
39
+ URIStr = HostStr.substring (hasURI);
40
+ HostStr = HostStr.substring (0 , hasURI);
41
+ }
42
+
43
+ String redirectRequest = methodStr + " " + URIStr;
44
+ if (searchStr!=" " ) redirectRequest += " ?" + searchStr;
45
+ redirectRequest += " " + versionStr;
46
+
47
+ uint8_t dataBuffer[1024 ];
48
+ String dataBufferStr;
49
+ int Rsize=0 ;
50
+ int Wsize=0 ;
51
+ bool header=true ;
52
+ unsigned long timeout;
53
+
54
+ // connect to the redirection client
55
+ WiFiClient redirectclient;
56
+ if (!redirectclient.connect (HostStr.c_str (), 80 )) {
57
+ Serial.println (" >>> Redirect connection failed" );
58
+ return false ;
59
+ }
60
+ redirectclient.print (redirectRequest+" \r\n " ); // SEND HTTP REQUEST
61
+
62
+
63
+ // wait for request availability
64
+ timeout = millis ();
65
+ while (client.available () == 0 ) {
66
+ if (millis () - timeout > 5000 ) {
67
+ Serial.println (" >>> Client Timeout !" );
68
+ client.stop ();
69
+ return false ;
70
+ }
71
+ }
72
+
73
+
74
+ // Read all the lines of the request to server and redirect it
75
+ while (client.available ()){
76
+ Rsize = client.readBytesUntil (' \n ' , dataBuffer, 1023 );
77
+
78
+ dataBuffer[Rsize] = ' \0 ' ; // to allow conversion to string
79
+ dataBufferStr = (const char *) dataBuffer;
80
+ if (header){
81
+ if (dataBufferStr.startsWith (" Host:" )){ // correct host to account for redirection
82
+ dataBufferStr = " Host: " + HostStr + " \r " ;
83
+ dataBufferStr.toCharArray ((char *)dataBuffer, 1023 );
84
+ Rsize = dataBufferStr.length ();
85
+ }else if (dataBufferStr==" \r " ){
86
+ header=false ; // end of header
87
+ }
88
+ }
89
+
90
+ dataBuffer[Rsize] = ' \n ' ; // add the separating char and adapt the Rsize accordingly
91
+ Rsize += 1 ;
92
+ if ((Rsize)!=redirectclient.write ((const uint8_t *)dataBuffer, Rsize)){
93
+ Serial.println (" >>> Mismatch in number of read/write bytes in http redirection !" );
94
+ break ;
95
+ }
96
+ }
97
+
98
+ // wait for response availability
99
+ timeout = millis ();
100
+ while (redirectclient.available () == 0 ) {
101
+ if (millis () - timeout > 5000 ) {
102
+ Serial.println (" >>> Redirect Client Timeout !" );
103
+ redirectclient.stop ();
104
+ return false ;
105
+ }
106
+ }
107
+
108
+
109
+ // Read all the lines of the reply from server and print them to Serial
110
+ while (redirectclient.available ()){
111
+ Rsize = redirectclient.read (dataBuffer, 1023 );
112
+ if (Rsize!=client.write ((const uint8_t *)dataBuffer, Rsize)){
113
+ Serial.println (" >>> Mismatch in number of read/write bytes in http response redirection !" );
114
+ break ;
115
+ }
116
+ }
117
+ redirectclient.stop ();
118
+ client.stop ();
119
+
120
+ return true ;
121
+ }
122
+
123
+
34
124
static char * readBytesWithTimeout (WiFiClient& client, size_t maxLength, size_t & dataLength, int timeout_ms)
35
125
{
36
126
char *buf = nullptr ;
@@ -91,7 +181,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
91
181
String searchStr = " " ;
92
182
int hasSearch = url.indexOf (' ?' );
93
183
if (hasSearch != -1 ){
94
- searchStr = urlDecode ( url.substring (hasSearch + 1 ) );
184
+ searchStr = url.substring (hasSearch + 1 );
95
185
url = url.substring (0 , hasSearch);
96
186
}
97
187
_currentUri = url;
@@ -111,6 +201,19 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
111
201
}
112
202
_currentMethod = method;
113
203
204
+ // allows http redirection with URL of the form:
205
+ // http://SERVERIP/fwd/REDIRECTIP --> very useful in meshnetwork architecture
206
+ // http://192.168.0.7/fwd/192.168.4.2 --> for instance, this allows to connect the chip with IP 192.168.4.2 which is connected to the softAP of node 192.168.0.7
207
+ // (POST requests are also supported)
208
+ if (url.indexOf (" /fwd/" ) == 0 ){
209
+ return redirectRequest (client, methodStr, url, searchStr, req.substring (addr_end + 1 ));
210
+ }
211
+
212
+ // decode the URL
213
+ searchStr = urlDecode (searchStr)); | searchStr = url.substring (hasSearch + 1 );
214
+
215
+
216
+
114
217
#ifdef DEBUG_ESP_HTTP_SERVER
115
218
DEBUG_OUTPUT.print (" method: " );
116
219
DEBUG_OUTPUT.print (methodStr);
0 commit comments