Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce code duplications in _parseRequest #6951

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

dirkmueller
Copy link
Contributor

This function had two loops for parsing headers, one used for GET
like requests and one for POST like request. we can combine them
in one, which reduces PROGMEM footprint by over 250 bytes.

Copy link
Collaborator

@earlephilhower earlephilhower left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious about the headerValue change. Rest seems quite nice!

libraries/ESP8266WebServer/src/Parsing-impl.h Outdated Show resolved Hide resolved
@@ -183,7 +183,7 @@ class ESP8266WebServerTemplate
void _uploadWriteByte(uint8_t b);
uint8_t _uploadReadByte(ClientType& client);
void _prepareHeader(String& response, int code, const char* content_type, size_t contentLength);
bool _collectHeader(const char* headerName, const char* headerValue);
bool _collectHeader(const String& headerName, const String& headerValue);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a signature change for a non-private method, and therefore breaking. We can wait to merge for v3, or you could add a compatibility method that forwards from one to the other.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is source compatible (String(const char*) constructor is implicit). I don't see the need to add a forwarder method - it will just work in any case as far as I can see.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're thinking of method call, I'm referring to inheritance. This change can cause a derived class to no longer compile. Trivial example with std::string instead of String, but the principle is the same:

struct Base
{
    void method(const char * arg) {std::cout << "Base arg=" << arg << std::endl;}
};

struct Derived : Base
{
//    void method(const char *arg) 
    void method(const std::string &arg) 
    {
        std::cout << "strlen=" << strlen(arg) << std::endl;
        Base::method(arg);
    }
};

libraries/ESP8266WebServer/src/Parsing-impl.h Show resolved Hide resolved

String formData;
// below is needed only when POST type request
if (method == HTTP_POST || method == HTTP_PUT || method == HTTP_PATCH || method == HTTP_DELETE){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you test that moving this condition still results in correct function as before for all methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not test all methods. I did test GET and POST though in the scope of my application and it worked fine.

Another way to put it is however that this "if condition" didn't actually move. what happened is that the header parsing part that was used in the POST/PUT/PATCH/DELETE code was moved up front, and then the rest of the code remained the same. this allowed to remove the header parsing that was done for GET/HEAD case as it was duplicate now.

Personally I think thats a relatively low risk change because the two header parsing loops were doing mostly the same, except that POST/PUT/PATCH/DELETE was checking for additional headers and parsed them. It now does so in all cases, but since it is extremely unlikely to get the additional header fields send by GET/HEAD requests, it will not matter in practice. and even if we get them, the parsed result is ignored as the if() condition remained the same and the parsed resulting values are not further evaluated.

@devyte
Copy link
Collaborator

devyte commented Jul 12, 2020

@dirkmueller we're now merging for v3, so it's ok to break things a bit. What's the status here? Did you get to testing all the methods?

@dirkmueller
Copy link
Contributor Author

yes, I tested this back then successfully. I'll have to rebase and let you know how retesting goes, currently working on another project.

This function had two loops for parsing headers, one used for GET
like requests and one for POST like request. we can combine them
in one, which reduces PROGMEM footprint by over 250 bytes.
@earlephilhower earlephilhower added the merge-conflict PR has a merge conflict that needs manual correction label Jan 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge-conflict PR has a merge conflict that needs manual correction
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants