Reem M
/
http-server-example1
http
source/main.cpp@1:ce6ccd14af4c, 2019-04-15 (annotated)
- Committer:
- reeml3
- Date:
- Mon Apr 15 18:12:24 2019 +0000
- Revision:
- 1:ce6ccd14af4c
- Parent:
- 0:a49e37a83a7a
http
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DuaaAbusharkh | 0:a49e37a83a7a | 1 | #include "mbed.h" |
DuaaAbusharkh | 0:a49e37a83a7a | 2 | #include "easy-connect.h" |
DuaaAbusharkh | 0:a49e37a83a7a | 3 | #include "http_server.h" |
DuaaAbusharkh | 0:a49e37a83a7a | 4 | #include "http_response_builder.h" |
DuaaAbusharkh | 0:a49e37a83a7a | 5 | |
DuaaAbusharkh | 0:a49e37a83a7a | 6 | |
DuaaAbusharkh | 0:a49e37a83a7a | 7 | |
DuaaAbusharkh | 0:a49e37a83a7a | 8 | Serial pc(USBTX,USBRX,9600); |
DuaaAbusharkh | 0:a49e37a83a7a | 9 | DigitalOut led(LED1); |
DuaaAbusharkh | 0:a49e37a83a7a | 10 | |
DuaaAbusharkh | 0:a49e37a83a7a | 11 | // Requests come in here |
DuaaAbusharkh | 0:a49e37a83a7a | 12 | |
DuaaAbusharkh | 0:a49e37a83a7a | 13 | void request_handler(ParsedHttpRequest* request, TCPSocket* socket) { |
DuaaAbusharkh | 0:a49e37a83a7a | 14 | |
DuaaAbusharkh | 0:a49e37a83a7a | 15 | pc.printf("Request came in: %s %s\n", http_method_str(request->get_method()), request->get_url().c_str()); |
DuaaAbusharkh | 0:a49e37a83a7a | 16 | |
DuaaAbusharkh | 0:a49e37a83a7a | 17 | if (request->get_method() == HTTP_GET && request->get_url() == "/") { |
DuaaAbusharkh | 0:a49e37a83a7a | 18 | HttpResponseBuilder builder(200); |
DuaaAbusharkh | 0:a49e37a83a7a | 19 | builder.set_header("Content-Type", "text/html; charset=utf-8"); |
DuaaAbusharkh | 0:a49e37a83a7a | 20 | |
DuaaAbusharkh | 0:a49e37a83a7a | 21 | char response[] = "<html><head><title>Hello from mbed</title></head>" |
DuaaAbusharkh | 0:a49e37a83a7a | 22 | "<body>" |
DuaaAbusharkh | 0:a49e37a83a7a | 23 | "<h1>mbed webserver</h1>" |
DuaaAbusharkh | 0:a49e37a83a7a | 24 | "<button id=\"toggle\">Toggle LED</button>" |
DuaaAbusharkh | 0:a49e37a83a7a | 25 | "<script>document.querySelector('#toggle').onclick = function() {" |
DuaaAbusharkh | 0:a49e37a83a7a | 26 | "var x = new XMLHttpRequest(); x.open('POST', '/toggle'); x.send();" |
DuaaAbusharkh | 0:a49e37a83a7a | 27 | "}</script>" |
DuaaAbusharkh | 0:a49e37a83a7a | 28 | "</body></html>"; |
DuaaAbusharkh | 0:a49e37a83a7a | 29 | |
DuaaAbusharkh | 0:a49e37a83a7a | 30 | builder.send(socket, response, sizeof(response) - 1); |
DuaaAbusharkh | 0:a49e37a83a7a | 31 | |
DuaaAbusharkh | 0:a49e37a83a7a | 32 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 33 | else if (request->get_method() == HTTP_POST && request->get_url() == "/toggle") { |
DuaaAbusharkh | 0:a49e37a83a7a | 34 | pc.printf("toggle LED called\n"); |
DuaaAbusharkh | 0:a49e37a83a7a | 35 | led = !led; |
DuaaAbusharkh | 0:a49e37a83a7a | 36 | |
DuaaAbusharkh | 0:a49e37a83a7a | 37 | HttpResponseBuilder builder(200); |
DuaaAbusharkh | 0:a49e37a83a7a | 38 | builder.send(socket, NULL, 0); |
DuaaAbusharkh | 0:a49e37a83a7a | 39 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 40 | else { |
DuaaAbusharkh | 0:a49e37a83a7a | 41 | HttpResponseBuilder builder(404); |
DuaaAbusharkh | 0:a49e37a83a7a | 42 | builder.send(socket, NULL, 0); |
DuaaAbusharkh | 0:a49e37a83a7a | 43 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 44 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 45 | |
DuaaAbusharkh | 0:a49e37a83a7a | 46 | int main() { |
DuaaAbusharkh | 0:a49e37a83a7a | 47 | |
reeml3 | 1:ce6ccd14af4c | 48 | lcd_printf("Reem"); |
DuaaAbusharkh | 0:a49e37a83a7a | 49 | // Connect to the network (see mbed_app.json for the connectivity method used) |
DuaaAbusharkh | 0:a49e37a83a7a | 50 | NetworkInterface* network = easy_connect(true); |
DuaaAbusharkh | 0:a49e37a83a7a | 51 | if (!network) { |
DuaaAbusharkh | 0:a49e37a83a7a | 52 | pc.printf("Cannot connect to the network, see serial output\n"); |
DuaaAbusharkh | 0:a49e37a83a7a | 53 | return 1; |
DuaaAbusharkh | 0:a49e37a83a7a | 54 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 55 | |
DuaaAbusharkh | 0:a49e37a83a7a | 56 | HttpServer server(network); |
DuaaAbusharkh | 0:a49e37a83a7a | 57 | nsapi_error_t res = server.start(8080, &request_handler); |
DuaaAbusharkh | 0:a49e37a83a7a | 58 | |
DuaaAbusharkh | 0:a49e37a83a7a | 59 | if (res == NSAPI_ERROR_OK) { |
DuaaAbusharkh | 0:a49e37a83a7a | 60 | pc.printf("Server is listening at http://%s:8080\n", network->get_ip_address()); |
DuaaAbusharkh | 0:a49e37a83a7a | 61 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 62 | else { |
DuaaAbusharkh | 0:a49e37a83a7a | 63 | pc.printf("Server could not be started... %d\n", res); |
DuaaAbusharkh | 0:a49e37a83a7a | 64 | } |
DuaaAbusharkh | 0:a49e37a83a7a | 65 | |
DuaaAbusharkh | 0:a49e37a83a7a | 66 | |
DuaaAbusharkh | 0:a49e37a83a7a | 67 | |
DuaaAbusharkh | 0:a49e37a83a7a | 68 | wait(osWaitForever); |
DuaaAbusharkh | 0:a49e37a83a7a | 69 | |
DuaaAbusharkh | 0:a49e37a83a7a | 70 | |
DuaaAbusharkh | 0:a49e37a83a7a | 71 | } |