X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=projects%2Fexample%2Fsample.net.simplehttpd%2Fsrc%2Fsketch.c;h=06530d3556b41be9dbfcd21ff4170293fa77391a;hb=6f577ed92e46bc8bf0463a7330ff698ec75853f2;hp=7166e6ba145c9f332b637a8625cf732aba3c0989;hpb=d9fc4960e76b75940daadc9b9f903cd50d1bc40a;p=mimic%2FMiMicSDK.git diff --git a/projects/example/sample.net.simplehttpd/src/sketch.c b/projects/example/sample.net.simplehttpd/src/sketch.c index 7166e6b..06530d3 100644 --- a/projects/example/sample.net.simplehttpd/src/sketch.c +++ b/projects/example/sample.net.simplehttpd/src/sketch.c @@ -3,365 +3,57 @@ * ROMにある定義済みファイルの入出力と、クロスドメインRESTが確認できます。 */ #include "boot/sketch.h" -#include "NyLPC_uipService.h" -#include "NyLPC_httpService.h" -#include "NyLPC_utils.h" -#include +#include "NyLPC_net.h" -#include "../api/NyLPC_cNet.h" -#include "../api/NyLPC_cHttpd.h" -#include "../api/NyLPC_cModShortUrl.h" +#include "NyLPC_http.h" +#include "NyLPC_utils.h" +#include "../netif/mimicip/NyLPC_cMiMicIpNetIf.h" -NyLPC_TcNet_t net; +const struct NyLPC_TiNetInterface_Interface* netif; struct MyHttpd{ NyLPC_TcHttpd_t super; }httpd; -NyLPC_TcModShortUrl_t mod_surl; +#define NUM_OF_FSDATA 4 +extern struct NyLPC_TRomFileData fsdata[]; + void handler(NyLPC_TcHttpdConnection_t* i_connection) { - NyLPC_THttpMethodType m; - NyLPC_cModShortUrl_parse(&mod_surl,i_connection); - m=NyLPC_cModShortUrl_getMethod(&mod_surl); - if(m!=NyLPC_THttpMethodType_HEAD || m!=NyLPC_THttpMethodType_GET){ - NyLPC_cHttpdConnection_sendResponseHeader(i_connection,200,"text/html",NULL); - } - if(m==NyLPC_THttpMethodType_GET){ - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection,""); - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection,"your request is %s",NyLPC_cModShortUrl_getPath(&mod_surl)); + NyLPC_TcModUrl_t mod; + char url[64]; + - NyLPC_cHttpdConnection_sendResponseBodyF(i_connection,""); + //SHORT URL + NyLPC_cModUrl_initialize(&mod); + if(!NyLPC_cModUrl_execute(&mod,i_connection,url,64)){ + NyLPC_cModUrl_finalize(&mod); + return; } + NyLPC_cHttpdConnection_sendResponseHeader(i_connection,200,"text/html",NULL); + NyLPC_cHttpdConnection_sendResponseBodyF(i_connection,"

Your request path is %s

",url); + NyLPC_cModUrl_finalize(&mod); return; } void setup(void) { //uipサービス初期化。いろいろ利用可能に。 - NyLPC_cNet_initialize(&net); + netif=NyLPC_cMiMicIpNetIf_getNetInterface(); + NyLPC_cNet_initialize(netif); NyLPC_cHttpd_initialize(&httpd.super,80); - httpd.super.function._handler=handler; - NyLPC_cModShortUrl_initialize(&mod_surl); + httpd.super.function.onRequest=handler; + } void loop(void) { NyLPC_TcNetConfig_t c2; - NyLPC_cNetConfig_initialize(&c2); - NyLPC_cNet_start(&net,&c2); + //192.168.0.39 + NyLPC_cNetConfig_initialize(&c2,NyLPC_TBool_TRUE); + NyLPC_cNet_start(&(c2.super)); NyLPC_cHttpd_loop(&httpd.super); for(;;); } -#ifdef COMMENT - - -//イーサネット用の初期化情報 -const struct NyLPC_TEthAddr ethaddr=NyLPC_TEthAddr_pack(0x02,0x01,0x02,0x03,0x04,0x05); -const struct NyLPC_TIPv4Addr ipaddr=NyLPC_TIPv4Addr_pack(192,168,0,39); -const struct NyLPC_TIPv4Addr netmask=NyLPC_TIPv4Addr_pack(255,255,255,0); -const struct NyLPC_TIPv4Addr gateway=NyLPC_TIPv4Addr_pack(192,168,0,254); - -//TCP処理スレッドの定義 -#define SIZE_OF_RX 256 -#define NUM_OF_TH 7 -struct TProc{ - NyLPC_TcThread_t th; - NyLPC_TcTcpSocket_t socket; - char rbuf[SIZE_OF_RX]; -}proc[NUM_OF_TH]; - -//ROMFSの定義 -extern struct NyLPC_TRomFileData file_cat_jpg; -extern struct NyLPC_TRomFileData file_index_html; -NyLPC_TcRomFileSet_t rfs; -const struct NyLPC_TRomFileData* rfsd[]={ - &file_index_html, - &file_cat_jpg}; - -//private 関数 - -static NyLPC_TUInt16 parseReqHeader(NyLPC_TcHttpStream_t* i_st,struct NyLPC_THttpShortRequestHeader* o_reqh); -static NyLPC_TBool writeError(NyLPC_TcHttpStream_t* i_st,const struct NyLPC_THttpBasicHeader* i_rqh,NyLPC_TUInt16 i_status); -static NyLPC_TBool writeFile(NyLPC_TcHttpStream_t* i_st,const struct NyLPC_THttpBasicHeader* i_rqh,const struct NyLPC_TRomFileData* i_file); -static NyLPC_TBool writeJson(NyLPC_TcHttpStream_t* i_st,const struct NyLPC_THttpBasicHeader* i_rqh); - - -//スケッチ - -void setup(void) -{ - int i; - //uipサービス初期化。いろいろ利用可能に。 - NyLPC_cUipService_initialize(); -// for(i=0;isocket),3000)){ - return -1; - } - num_of_connect++; - - //TCPのオープン - if(NyLPC_cHttpStream_initialize(&st,&(proc->socket))){ - for(;;){ - ret=parseReqHeader(&st,&reqheader); - num_of_reqest++; - //コネクションが増えすぎたら持続性接続を停止するために上書き。 - if(num_of_connect>5){ - reqheader.super.connection=NyLPC_THttpMessgeHeader_Connection_CLOSE; - } - if(ret!=200){ - //エラーならエラーレスポンスを生成。持続性接続しない。 - writeError(&st,&(reqheader.super),ret); - num_of_error++; - break; - } - //URLから判定。 - if(strncmp("/rf.api?",reqheader.url,8)==0){ - //ファイル検索 - rf=NyLPC_cRomFileSet_getFilaData(&rfs,reqheader.url+8); - if(rf==NULL){ - num_of_error++; - if(!writeError(&st,&(reqheader.super),404)){ - break; - } - }else{ - if(!writeFile(&st,&(reqheader.super),rf)){ - num_of_error++; - break; - } - } - }else if(strncmp("/status.json",reqheader.url,8)==0){ - if(!writeJson(&st,&(reqheader.super))){ - break; - } - //httpdの状態を返す。 - }else{ - if(!writeFile(&st,&(reqheader.super),NyLPC_cRomFileSet_getFilaData(&rfs,"index.html"))){ - num_of_error++; - break; - } - } - - } - NyLPC_cHttpStream_finalize(&st); - } - //5秒以内に切断 - NyLPC_cTcpSocket_close(&(proc->socket),5000); - num_of_connect--; - return 0; -} - - -void loop(void) -{ - NyLPC_TcIPv4Config_t config; - NyLPC_TcTcpListener_t listener; - int i; - - - NyLPC_cIPv4Config_initialzeForEthernet(&config,ðaddr,1480); - NyLPC_cIPv4Config_setDefaultRoute(&config,&gateway); - NyLPC_cIPv4Config_setIp(&config,&ipaddr,&netmask); - - -// NyLPC_cTcpListener_initialize(&listener,80); - NyLPC_cUipService_start(&config); -/* for(;;){ - //ターミネイト状態のタスクを検索 - for(i=0;isuper.type!=NyLPC_THttpHeaderType_REQUEST){ - ret=400; - } - //GETだけネ - if(o_reqh->super.startline.req.method!=NyLPC_THttpMethodType_GET){ - ret=405; - } - NyLPC_cHttpBasicHeaderParser_finalize(&hp); - return ret; -} -/** - * エラーレスポンスのライタ。 - * 戻り値はpersistentConnectionが有効かどうか。 - */ -static NyLPC_TBool writeError(NyLPC_TcHttpStream_t* i_st,const struct NyLPC_THttpBasicHeader* i_rqh,NyLPC_TUInt16 i_status) -{ - static const char* HTML_FORMAT="

STATUS %d


"NyLPC_cHttpdConfig_SERVER""; - NyLPC_TcHttpHeaderWriter_t hw; - NyLPC_TcHttpBodyWriter_t bw; - //ヘッダライタの生成 - if(!NyLPC_cHttpHeaderWriter_initialize(&hw,i_st,i_rqh)){ - return NyLPC_TBool_FALSE; - } - //ヘッダ書込み - if(!NyLPC_THttpBasicHeader_isPersistent(i_rqh)){ - NyLPC_cHttpHeaderWriter_setClose(&hw); - } - //@bug HTTP/1.1未満のクライアントを考慮していない。 - NyLPC_cHttpHeaderWriter_setChunked(&hw); - //ヘッダの基本情報出力 - NyLPC_cHttpHeaderWriter_writeHeader(&hw,i_status); - //拡張メッセージヘッダの出力 - NyLPC_cHttpHeaderWriter_writeMessage(&hw,"Content-type","text/html"); - - //ヘッダ書込み終わり。(最後だけチェック) - if(!NyLPC_cHttpHeaderWriter_close(&hw)){ - NyLPC_cHttpHeaderWriter_finalize(&hw); - return NyLPC_TBool_FALSE; - } - NyLPC_cHttpHeaderWriter_finalize(&hw); - - //Bodyの書込み - NyLPC_cHttpBodyWriter_initialize(&bw,i_st); - //チャンク転送設定 - NyLPC_cHttpBodyWriter_setChunked(&bw); - NyLPC_cHttpBodyWriter_format(&bw,HTML_FORMAT,(NyLPC_TInt32)i_status); - //エラーチェック - if(!NyLPC_cHttpBodyWriter_close(&bw)){ - NyLPC_cHttpBodyWriter_finalize(&hw); - return NyLPC_TBool_FALSE; - } - NyLPC_cHttpBodyWriter_finalize(&hw); - return NyLPC_THttpBasicHeader_isPersistent(i_rqh); -} - -/** - * エラーレスポンスのライタ。 - * 戻り値はpersistentConnectionが有効かどうか。 - */ -static NyLPC_TBool writeJson(NyLPC_TcHttpStream_t* i_st,const struct NyLPC_THttpBasicHeader* i_rqh) -{ - static const char* JSON_FORMAT="{nr:%d,ne:%d,ac:%d,er:\"%d/%d/%d\",tx:%d}"; - NyLPC_TcHttpHeaderWriter_t hw; - NyLPC_TcHttpBodyWriter_t bw; - //ヘッダライタの生成 - if(!NyLPC_cHttpHeaderWriter_initialize(&hw,i_st,i_rqh)){ - return NyLPC_TBool_FALSE; - } - //ヘッダ書込み - if(!NyLPC_THttpBasicHeader_isPersistent(i_rqh)){ - NyLPC_cHttpHeaderWriter_setClose(&hw); - } - //@bug HTTP/1.1未満のクライアントを考慮していない。 - NyLPC_cHttpHeaderWriter_setChunked(&hw); - //ヘッダの基本情報出力 - NyLPC_cHttpHeaderWriter_writeHeader(&hw,200); - //拡張メッセージヘッダの出力 - NyLPC_cHttpHeaderWriter_writeMessage(&hw,"Content-type","application/json"); - NyLPC_cHttpHeaderWriter_writeMessage(&hw,"Access-Control-Allow-Origin","*"); - - //ヘッダ書込み終わり。(最後だけチェック) - if(!NyLPC_cHttpHeaderWriter_close(&hw)){ - NyLPC_cHttpHeaderWriter_finalize(&hw); - return NyLPC_TBool_FALSE; - } - NyLPC_cHttpHeaderWriter_finalize(&hw); - - //Bodyの書込み - NyLPC_cHttpBodyWriter_initialize(&bw,i_st); - //チャンク転送設定 - NyLPC_cHttpBodyWriter_setChunked(&bw); - NyLPC_cHttpBodyWriter_format(&bw,JSON_FORMAT, - (NyLPC_TInt32)num_of_reqest,(NyLPC_TInt32)num_of_error,(NyLPC_TInt32)num_of_connect, - NyLPC_assert_counter,NyLPC_abort_counter,NyLPC_debug_counter,dbg_getNumofUsedTx()); - //エラーチェック - if(!NyLPC_cHttpBodyWriter_close(&bw)){ - NyLPC_cHttpBodyWriter_finalize(&hw); - return NyLPC_TBool_FALSE; - } - NyLPC_cHttpBodyWriter_finalize(&hw); - return NyLPC_THttpBasicHeader_isPersistent(i_rqh); -} -/** - * ROMファイルのレスポンスライタ。 - * 戻り値はpersistentConnectionが有効かどうか。 - */ -static NyLPC_TBool writeFile(NyLPC_TcHttpStream_t* i_st,const struct NyLPC_THttpBasicHeader* i_rqh,const struct NyLPC_TRomFileData* i_file) -{ - NyLPC_TcHttpHeaderWriter_t hw; - NyLPC_TcHttpBodyWriter_t bw; - //ヘッダライタの生成 - if(!NyLPC_cHttpHeaderWriter_initialize(&hw,i_st,i_rqh)){ - return NyLPC_TBool_FALSE; - } - //ヘッダ書込み - if(!NyLPC_THttpBasicHeader_isPersistent(i_rqh)){ - NyLPC_cHttpHeaderWriter_setClose(&hw); - } - NyLPC_cHttpHeaderWriter_setContentLength(&hw,i_file->size); - //ヘッダの基本情報出力 - NyLPC_cHttpHeaderWriter_writeHeader(&hw,200); - //拡張メッセージヘッダの出力 - NyLPC_cHttpHeaderWriter_writeMessage(&hw,"Content-type",i_file->content_type); - - //ヘッダ書込み終わり。(最後だけチェック) - if(!NyLPC_cHttpHeaderWriter_close(&hw)){ - NyLPC_cHttpHeaderWriter_finalize(&hw); - return NyLPC_TBool_FALSE; - } - NyLPC_cHttpHeaderWriter_finalize(&hw); - - //Bodyの書込み - NyLPC_cHttpBodyWriter_initialize(&bw,i_st); - NyLPC_cHttpBodyWriter_write(&bw,i_file->data,i_file->size); - //エラーチェック - if(!NyLPC_cHttpBodyWriter_close(&bw)){ - NyLPC_cHttpBodyWriter_finalize(&hw); - return NyLPC_TBool_FALSE; - } - NyLPC_cHttpBodyWriter_finalize(&hw); - return NyLPC_THttpBasicHeader_isPersistent(i_rqh); -} -#endif