Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

WebServer Methods Questions #7656

Unanswered
Xylopyrographer asked this question in Q&A
Discussion options

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:

  1. What is the default header sent when using send()? (Assuming no calls to sendHeader() have been made previously.)

Regarding sendHeader(const String& name, const String& value, bool first = false) method.

  1. What is the effect on the header of using the first parameter? Does it change the order of elements assembled by previous sendHeader() calls?
    Edit: In looking at WebServer.cpp, confirming that first 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.

  1. For the method parameter, most examples use HTTP_POST or HTTP_GET. What do these expand to and where are they enumerated?

  2. 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 after ufn returns? but don't see what triggers that call, since the entire on statement is only executed if the endpoint is matched and is of type HTTP_POST.

  3. 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.

@igrr @SuGlider

You must be logged in to vote

Replies: 2 comments

Comment options

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 .

You must be logged in to vote
0 replies
Comment options

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.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /