00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "httpd.h"
00032 #include "dispatcher.h"
00033 #include "inet.h"
00034 #include "os_core.h"
00035 #include "nip_error.h"
00036 #include "net/net_if.h"
00037 #include "net/tcp.h"
00038 #include "net/numbers.h"
00039 #include "app/ports.h"
00040 #include "app/dns_sd.h"
00041
00042 #if HTTPD_ENABLE == 1
00043
00044 nip_tcp_sock_id_t http_listensock;
00045 nip_tcp_sock_id_t http_opensock = NIP_TCP_NO_SOCKET;
00046
00047
00048 void httpd_start( void )
00049 {
00050 struct nip_tcp_sock_addr sockaddr;
00051
00052
00053 nip_memset( &sockaddr, 0, sizeof( struct nip_tcp_sock_addr ) );
00054 sockaddr.port = NIP_TCP_PORT_HTTP;
00055 http_listensock = nip_tcp_socket( &sockaddr, NIP_TCP_SOCK_FLG_LISTENING );
00056 }
00057
00058 void httpd_check( void )
00059 {
00060 nip_mem_size_t size;
00061 uint8_t buf;
00062 uint8_t p;
00063 static uint8_t found_addr;
00064 static http_state_t state;
00065 static uint8_t nl_pos;
00066 static struct http_parser parser[HTTP_PARSER_COUNT] = {
00067 {0, 6, (uint8_t*)"GET /A" },
00068 {0, 6, (uint8_t*)"GET /B" },
00069 {0, 2, (uint8_t*)"\r\n"}};
00070 #if (NIP_DNSSD_ENABLE == 1)
00071 nip_dnssd_id_t srv_id = NIP_DNSSD_NO_SERVICE;
00072 uint8_t *instance_name = (uint8_t*)"\x07""Boogie!";
00073 uint8_t *service_name = (uint8_t*)"\x05""_http""\x04""_tcp";
00074 uint8_t *txt_records = (uint8_t*)"\x07""path=/B";
00075 nip_mem_ptr_t in_mem_ptr;
00076 nip_mem_ptr_t srv_mem_ptr;
00077 nip_mem_ptr_t txt_mem_ptr;
00078 #endif
00079
00080 #if (NIP_DNSSD_ENABLE == 1)
00081
00082 if ( srv_id == NIP_DNSSD_NO_SERVICE )
00083 {
00084 in_mem_ptr.id = NIP_MEM_NULL;
00085 in_mem_ptr.ptr = instance_name;
00086
00087 srv_mem_ptr.id = NIP_MEM_NULL;
00088 srv_mem_ptr.ptr = service_name;
00089
00090
00091 txt_mem_ptr.id = NIP_MEM_NULL;
00092 txt_mem_ptr.ptr = txt_records;
00093
00094 srv_id = nip_dnssd_register( &in_mem_ptr, &srv_mem_ptr, &txt_mem_ptr,8, NIP_TCP_PORT_HTTP, 0, 0 );
00095 }
00096 #endif
00097
00098
00099 if ( http_opensock == NIP_TCP_NO_SOCKET )
00100 {
00101 http_opensock = nip_tcp_accept( http_listensock );
00102 if ( http_opensock != NIP_TCP_NO_SOCKET )
00103 {
00104 nl_pos = 0;
00105 parser[0].pos = 0;
00106 parser[1].pos = 0;
00107 parser[2].pos = 0;
00108 state = HTTP_STAT_ADDR;
00109 }
00110 }
00111
00112 if ( http_opensock != NIP_TCP_NO_SOCKET )
00113 {
00114
00115 size = nip_tcp_read( http_opensock, &buf, 1 );
00116
00117
00118
00119
00120 if ( size == 0 && nip_error != NIP_E_AGAIN )
00121 {
00122
00123 if ( state != HTTP_STAT_CLOSE )
00124 nip_tcp_close( http_opensock );
00125
00126 http_opensock = NIP_TCP_NO_SOCKET;
00127 }
00128
00129 else if ( size > 0 && state != HTTP_STAT_CLOSE )
00130 {
00131
00132 nl_pos++;
00133 p = 0;
00134 do
00135 {
00136
00137
00138
00139 if ( buf == parser[p].s[parser[p].pos] )
00140 parser[p].pos++;
00141 else
00142 parser[p].pos = 0;
00143
00144
00145 if ( state == HTTP_STAT_ADDR
00146 && p <= HTTP_ADDR_PARSER
00147 && parser[p].pos == parser[p].sz )
00148 {
00149 state = HTTP_STAT_200;
00150 found_addr = p;
00151 }
00152
00153
00154 if ( p == HTTP_NL_PARSER && parser[p].pos == 2 )
00155 {
00156
00157 if ( state == HTTP_STAT_ADDR )
00158 state = HTTP_STAT_404;
00159
00160 if ( nl_pos == parser[p].pos )
00161 goto header_done;
00162
00163 parser[p].pos = 0;
00164 nl_pos = 0;
00165 }
00166
00167 }
00168 while ( ++p < HTTP_PARSER_COUNT );
00169
00170 }
00171 }
00172
00173 goto quit;
00174
00175 header_done:
00176
00177
00178 if ( state == HTTP_STAT_404 )
00179 {
00180 nip_tcp_write( http_opensock, (uint8_t*)"HTTP/1.0 404\r\ncontent-type:text/html\r\n\r\n", 40 );
00181 nip_tcp_write( http_opensock, (uint8_t*)"<h1>404 Not Found</h1>", 22 );
00182 }
00183
00184 if ( state == HTTP_STAT_200 )
00185 {
00186
00187 nip_tcp_write( http_opensock, (uint8_t*)"HTTP/1.0 200\r\ncontent-type:", 27 );
00188 if ( found_addr == 0 )
00189 {
00190 nip_tcp_write( http_opensock, (uint8_t*)"text/plain\r\n\r\n", 14);
00191 buf = 250;
00192 nip_tcp_write( http_opensock, &buf, 1 );
00193 }
00194 else if ( found_addr == 1 )
00195 nip_tcp_write( http_opensock, (uint8_t*)"text/html\r\n\r\n<h1>Ooh Yes!! Boogie!!</h1>", 40 );
00196 }
00197
00198 nip_tcp_close( http_opensock );
00199 state = HTTP_STAT_CLOSE;
00200
00201 quit:
00202 return;
00203 }
00204
00205 #endif