-
Notifications
You must be signed in to change notification settings - Fork 7.7k
WebServer Methods Questions #7656
-
I'm looking to better understand what is going on with a few of the WebServer library methods.
Referring to the WebUpdate example:
Regarding the send()
methods:
- What is the default header sent when using
send()
? (Assuming no calls tosendHeader()
have been made previously.)
Regarding sendHeader(const String& name, const String& value, bool first = false)
method.
- What is the effect on the header of using the
first
parameter? Does it change the order of elements assembled by previoussendHeader()
calls?
Edit: In looking at WebServer.cpp, confirming thatfirst
sets this header at the top of the response header string.
Regarding on(const Uri &uri, HTTPMethod method, THandlerFunction fn, THandlerFunction ufn)
here:
As used in line 30, it has 4 parameters.
-
For the
method
parameter, most examples useHTTP_POST
orHTTP_GET
. What do these expand to and where are they enumerated? -
Trying to understand the 4th parameter,
ufn
. It seems to be the function called to handle file uploads. But under what conditions, or what triggers it to be called instead of the 3rd parameter,fn
?In the example,
fn
is called afterufn
returns? but don't see what triggers that call, since the entireon
statement is only executed if the endpoint is matched and is of typeHTTP_POST
. -
Why prefer that method over using
onFileUpload(THandlerFunction fn)
? here
Looks like WebServer might be a candidate for refactoring as per #6759, but in the meantime any insight would be appreciated.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments
-
These are function prototype of send()
图片
As you can see, the String header
is the header that need to send
图片
and then you can see the _prepareHeader
function
void WebServer::_prepareHeader(String& response, int code, const char* content_type, size_t contentLength) {
response = String(F("HTTP/1.")) + String(_currentVersion) + ' ';
response += String(code);
response += ' ';
response += _responseCodeToString(code);
response += "\r\n";
using namespace mime;
if (!content_type)
content_type = mimeTable[html].mimeType;
sendHeader(String(F("Content-Type")), String(FPSTR(content_type)), true);
if (_contentLength == CONTENT_LENGTH_NOT_SET) {
sendHeader(String(FPSTR(Content_Length)), String(contentLength));
} else if (_contentLength != CONTENT_LENGTH_UNKNOWN) {
sendHeader(String(FPSTR(Content_Length)), String(_contentLength));
} else if(_contentLength == CONTENT_LENGTH_UNKNOWN && _currentVersion){ //HTTP/1.1 or above client
//let's do chunked
_chunked = true;
sendHeader(String(F("Accept-Ranges")),String(F("none")));
sendHeader(String(F("Transfer-Encoding")),String(F("chunked")));
}
if (_corsEnabled) {
sendHeader(String(FPSTR("Access-Control-Allow-Origin")), String("*"));
sendHeader(String(FPSTR("Access-Control-Allow-Methods")), String("*"));
sendHeader(String(FPSTR("Access-Control-Allow-Headers")), String("*"));
}
sendHeader(String(F("Connection")), String(F("close")));
response += _responseHeaders;
response += "\r\n";
_responseHeaders = "";
}
And I think those two functions above will answer your questions 1 and 2 .
Beta Was this translation helpful? Give feedback.
All reactions
-
For question 4, I would like to say if you are using Arduino 2.0.x
or Platform IO
, you can hold left CTRL
and click these two variables HTTP_POST
, HTTP_GET
. Arduino 2.0.x requires you to first verify and build so that the IntelliSense feature can be turned on.
Question 5 may be solved by yourself if you can directly see the source code. For the part where you say how to trigger ufn
and fn
. Based on my understanding, HTTP is like this: your mom calls you to eat dinner, and then you respond.
So, assuming fn
is the sending request function, and ufn
is the hearing back function. You send the request to the server via fn
, and the server gives your response, handled by ufn
.
I may not be correct, but this is what I have understood so far. Any mistakes please point them out because I am still learning. Thanks.
Beta Was this translation helpful? Give feedback.