-
Notifications
You must be signed in to change notification settings - Fork 26
Add HTTPServer example #17
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
base: master
Are you sure you want to change the base?
Conversation
@kegilbert Great idea, happy to see this included.
Note that I've added Greentea tests to Mbed HTTP recently. Could we included them in the CI as well for this repo? |
One of the things I would consider could use improving is to use the asynchronous socket interface for everything. I think we could get rid of the threads. Also, I'd suggest to put the stats on a web page, instead of on the console. That'd be more fun. |
Is the aim to publish this as a official example? I would prefer this not to be part of official Handbook, but instead published somewhere in mbed.org, or do we have some kind of Cookbook. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like the approach where the code lives inside header files.
Can you move the HTTPresponseparser and server to .cpp files?
printf("Server could not be started... %d\n", res); | ||
} | ||
|
||
wait(osWaitForever); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this is here?
If we rely on event-loop to run, and main() has nothing else to do, can we just same some RAM and run event-loop from main thread?
@SeppoTakalo It lives at https://os.mbed.com/teams/sandbox/code/http-webserver-example/ currently. |
I think that is the proper location for it. We should have clear distinction between what examples are part of API handbook, and what are just examples build on top of Mbed OS 5. We don't have HTTP library, and therefore this example should not end up in the handbook. But we should create some kind of Cookbook (did we already have it at some point?) that demonstrates all kind of cool things that you could build on top of Mbed OS. |
@SeppoTakalo Good point, there's ongoing work this release to expand and unify our examples. We don't really have many examples now that build interesting user applications off of Mbed OS, and as such we don't have a proper location yet for them to live. Moving forward the plan is to add a section to the Handbook Tutorial page that includes more varied examples. These examples would live in a consolidated Github repo allowing CI, OS release updates, and review processes to become standardized. I would like some of the examples to use libraries outside of Mbed OS as a way to showcase how to integrate other applications and allow some really interesting projects. This does add more maintenance problems though and am open to suggestions. We'll be having some internal discussions soon, I'll be sure to include both of you in the process. |
@ARMmbed/mbed-docs Thoughts? |
@SeppoTakalo it should be in the handbook (the cookbook is no longer supported), just not in the API references (that's for code snippets). We are absolutely looking for solid, interesting and useful examples to include in the docs. |
@iriark01 @SeppoTakalo @kegilbert Thoughts on moving this forward, or should this be closed? |
Sorry, I was waiting for @SeppoTakalo to reply. |
Also, @AnotherButler is collecting these |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK,
If this is going to go into the handbook, and we don't have the cookbook anymore, then I need to request some changes to it.
- Documentation is outdated, it refers to easy-connect, but does not use it. It also mentions outdated ESP8266 patches.
mbed_app.json
defines parameters that are not used in this app. Those were used by easy-connect.- Implementation needs to go into
.cpp
files. We should not have all the code in header files. - Other small changes suggested.
Other than those, this is a good example.
Who is maintaining tha HTTP library that this depends on?
HTTPServer/http_server.h
Outdated
if (recv_ret < -3000 || recv_ret == 0) { | ||
return; | ||
} else { | ||
continue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this sequence supposed to do?
Any negative value should be handled as a error. You should not continue reading the socket anymore.
https://github.com/ARMmbed/mbed-os/blob/master/features/netsocket/Socket.h#L108..L113
HTTPServer/LICENSE
Outdated
@@ -0,0 +1,191 @@ | |||
Apache License |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need full license text? Other examples do not have it, they just refer to Apache 2.0 in the source files.
HTTPServer/README.md
Outdated
|
||
## To build | ||
|
||
1. Open ``mbed_app.json`` and change the `network-interface` option to your connectivity method ([more info](https://github.com/ARMmbed/easy-connect)). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not prefer to use easy-connect. It is not something we maintain.
HTTPServer/mbed_app.json
Outdated
"mbed-http.http-buffer-size": 2048, | ||
"platform.stdio-baud-rate": 115200, | ||
"platform.thread-stats-enabled": 1, | ||
"nsapi.socket-stats-enable": 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would convert this example to use the default network interface and limit this file to only
{
"target_overrides": {
"*": {
"mbed-http.http-buffer-size": 2048,
"platform.stdio-baud-rate": 115200,
"platform.thread-stats-enabled": 1,
"nsapi.socket-stats-enable": 1
}
}
}
|
||
// Connect to the network (see mbed_app.json for the connectivity method used) | ||
NetworkInterface *network = NetworkInterface::get_default_instance(); | ||
if (!network) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NetworkInterface::get_default_instance()
will give you the interface, but it does not connect.
You need to also add network->connect()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The HttpServer class constructor connects with the provided interface.
|
||
* K64F with Ethernet. | ||
* NUCLEO_F411RE with ESP8266. | ||
* For ESP8266, you need [this patch](https://github.com/ARMmbed/esp8266-driver/pull/41). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is outdated, the external repository is not used anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still valid.
HTTPServer/http_response_builder.h
Outdated
#include "http_parser.h" | ||
#include "http_parsed_url.h" | ||
|
||
static const char *get_http_status_string(uint16_t status_code) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Static functions in header files?
Apologies for the delay! I've done some work to remove references to easy-connect and some minor cleanup. I'm a bit tied up at the moment with the example consolidation project but I'll get back to this ASAP to resolve Seppo's other comments. Thanks for the review! |
@kegilbert Ping |
Sorry about that, flu and example consolidation sort of knocked me out for a good while. Hoping to polish this off end of this week/beginning of next week. |
Large scale refactor finished and ran through astyle. Still need to clean up some comments but functionally mostly there. @janjongboom I poked around a bit with getting the thread stats to display on the webpage but my Javascript fu was a bit lacking. I wasn't able to get the XMLHttpRequest onstagechange to work correctly (think the object was being killed as it fell out of scope before the response could be sent). I can poke around a bit more later if you think it'd be worth it. |
@kegilbert Reviewed, and added some changes here: kegilbert#1 |
Make http-server look pretty
@janjongboom Merged and test with three concurrent connections. Looks pretty awesome! My only concern is the addition of another external library, who maintains rapidjson? |
@kegilbert Tencent. It's not an Mbed specific library. |
@SeppoTakalo @janjongboom Any further thoughts/feedback? Going forward into 5.13 this will be tested along with every other example. Is the Tencent RapidJSON library a stable enough product do you think to not impact CI? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small changes requested.
I think this is now leaking memory.
m->thread = t; | ||
socket_threads.push_back(m); | ||
} else { | ||
delete clt_sock; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Delete nullptr
?
This else-block does not have any effect.
|
||
* K64F with Ethernet. | ||
* NUCLEO_F411RE with ESP8266. | ||
* For ESP8266, you need [this patch](https://github.com/ARMmbed/esp8266-driver/pull/41). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still valid.
|
||
void HttpServer::receive_data() | ||
{ | ||
TCPSocket *socket = sockets.back(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you mean to use .pop_back()
, because now it looks like the sockets vector is growing constantly as I cannot find any place where sockets would be removed from there.
Updated version of: https://github.com/janjongboom/mbed-os-example-http-server/
Placing this example in the docs only repo as per previous discussions with @cmonr until the examples standardization work is further along.
Wanted some feedback from Jan and someone on the networking group (tagged Seppo for now). Please feel free to tag anyone else who may be interested. We don't have CI yet in this repo but I have run the code through astyle and built it with GCC_ARM, IAR, and ARM compilers.
@AnotherButler, this was one of the new examples we had discussed with @iriark01 and @SenRamakri