Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of PicoTCP by
pico_simple_http.c
00001 /********************************************************************* 00002 PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved. 00003 See LICENSE and COPYING for usage. 00004 00005 Author: Andrei Carp <andrei.carp@tass.be> 00006 *********************************************************************/ 00007 00008 #include "pico_config.h" 00009 #include "pico_socket.h" 00010 #include "pico_tcp.h" 00011 #include "pico_ipv4.h" 00012 #include "pico_simple_http.h" 00013 00014 /* The HTTP Server cannot be available without TCP support */ 00015 #if (defined PICO_SUPPORT_HTTP) && (defined PICO_SUPPORT_IPV4) && (defined PICO_SUPPORT_TCP) 00016 00017 #define HTTP_LISTEN_PORT 80u 00018 #define HTTP_BACKLOG 5u 00019 #define HTTP_HEADER_SIZE 256u 00020 00021 #define HTTP_SUCCESS 0 00022 #define HTTP_ERROR -1 00023 00024 static struct pico_socket * httpServer = NULL; 00025 static char httpResponse[] = 00026 "HTTP/1.0 200 OK\r\n\ 00027 Content-Type: text/html\r\n\ 00028 \r\n\ 00029 <html><head>\r\n\ 00030 <title>picoTCP Simple Http server</title>\r\n\ 00031 </head>\r\n\ 00032 <body>\r\n\ 00033 <h1>Hello world from picoTCP !!</h1>\r\n\ 00034 </body>\r\n"; 00035 00036 static void httpEventCbk(uint16_t ev, struct pico_socket *self) 00037 { 00038 static struct pico_socket * client = NULL; 00039 uint32_t peer; 00040 uint16_t port; 00041 int r; 00042 char buffer[HTTP_HEADER_SIZE]; 00043 00044 switch(ev) 00045 { 00046 case PICO_SOCK_EV_CONN : 00047 if(!client) 00048 client = pico_socket_accept(self, &peer, &port); 00049 break; 00050 00051 case PICO_SOCK_EV_RD: 00052 // do not check http integrity, just mark that the http header has arrived 00053 // prepare to send the response 00054 r = pico_socket_recvfrom(self, buffer, HTTP_HEADER_SIZE, &peer, &port); 00055 if(r>0 && memcmp(buffer,"GET",3u) == 0u) 00056 { // it is an http header asking for data, return data and close 00057 pico_socket_write(self,httpResponse,sizeof(httpResponse)); 00058 pico_socket_close(self); 00059 } 00060 else 00061 { 00062 // kill the connection, invalid header 00063 pico_socket_close(self); 00064 } 00065 break; 00066 00067 case PICO_SOCK_EV_ERR: 00068 case PICO_SOCK_EV_CLOSE: 00069 // free the used socket 00070 client = NULL; 00071 break; 00072 00073 default : 00074 break; 00075 } 00076 } 00077 00078 int pico_startHttpServer(struct pico_ip4 * address) 00079 { 00080 00081 uint16_t localHttpPort = short_be(HTTP_LISTEN_PORT); 00082 00083 if(!pico_is_port_free(localHttpPort,PICO_PROTO_TCP, address, &pico_proto_ipv4)) 00084 { 00085 pico_err = PICO_ERR_EADDRINUSE; 00086 return HTTP_ERROR; 00087 } 00088 00089 httpServer = pico_socket_open(PICO_PROTO_IPV4, PICO_PROTO_TCP, httpEventCbk); 00090 00091 if(!httpServer) 00092 { 00093 pico_err = PICO_ERR_ENOMEM; 00094 return HTTP_ERROR; 00095 } 00096 00097 // both functions set the pico_err themselves. 00098 if(pico_socket_bind(httpServer,address,&localHttpPort)) 00099 return HTTP_ERROR; 00100 00101 if(pico_socket_listen(httpServer,HTTP_BACKLOG)) 00102 return HTTP_ERROR; 00103 00104 return HTTP_SUCCESS; 00105 } 00106 00107 int pico_stopHttpServer(void) 00108 { 00109 if(!httpServer) 00110 { 00111 pico_err = PICO_ERR_EINVAL; 00112 return HTTP_ERROR; 00113 } 00114 00115 if(pico_socket_close(httpServer)) 00116 { 00117 // no need to set the error here, function already set it 00118 httpServer = NULL; 00119 return HTTP_ERROR; 00120 } 00121 00122 httpServer = NULL; 00123 return HTTP_SUCCESS; 00124 } 00125 00126 #endif 00127 00128
Generated on Thu Jul 14 2022 08:24:59 by
1.7.2
