This is Webservice SDK for mbed. LPCXpresso1769/LPC1768/FRDM-K64F/LPC4088

Dependents:   MbedFileServer_1768MiniDK2 RedWireBridge IssueDebug_gcc MiMicRemoteMCU-for-Mbed ... more

libMiMic(MiMic library for mbed)は、WebService機能を提供するSDKです。 mbedでWebAPIに対応したネットワークデバイスを簡単に作ることが出来ます。

libMiMicはMiMic projectで開発しています。MiMic projectについてはこちらをご覧ください。 http://nyatla.jp/mimic/wp/

構成

libMiMicはmbedRTOS上で動作し、ユーザアプリケーションにAPIを提供します。コアAPIはC言語で記述されていますが、使用頻度の高いものについてはmbed向けのC++APIが準備されています。

/media/uploads/nyatla/libmimic-sdk.png

※libMiMicはmbedの標準イーサネットドライバをしようしていません。

標準イーサネットドライバと同時に使用することはできません。

  • MiMicIP - IPv4スタックです。レテンシとメモリ消費量を抑えたuipベースのライブラリです。
  • ARP/ICMP/UDP/TCP - 基礎的なソケットAPIを提供します。APIは独自です。
  • HTTP/1.1 Server - HTTP/1.1に対応したサーバです。マルチセッション・Chunked・持続性接続に対応しています。
  • HTTP Modules - HTTP/1.1の機能モジュールです。以下のモジュールがあります。
    • ROM file provider - ROMに格納したファイルイメージを公開します。
    • File system provider - mbedファイルシステムを公開します。
    • Onchip configuration - プログラムフラッシュを利用して設定を保存します。
    • MiMicVM processor - RPCリクエスト(MiMicVM)を処理します。
    • FileUpload - ファイルアップロードを受け取ります。
    • URL decoder - HTTPリクエストを解析します。
    • UPnP handler -UPnPメッセージを処理します。
    • WebSocket - Websocketサーバです。
  • mDNS - マルチキャストDNSサービスです。
  • UPnP - UPnP/1.0の機能を提供します。UPnP handlerと協調して動作します。(現在はデバイス探索(SSDP)・デスクリプション(Description)のみ実装してあります。)
  • DHCP/APIPA - ゼロコンフィギュレーション用のモジュールです。
  • HTTP/1.1 Client
  • mbed C++ class library - mbed向けのC++CPIです。C言語のものより簡単です。

対応機種

  • mbed(mbed LPC1768)
  • LPCXpresso1769

プログラム

Import programMiMicRemoteMCU-for-Mbed

MiMic RemoteMCU for mbed. This program provides MCU control API over REST API. It can control MCU from Javascript,PHP or any HTTP rest client directly. And, The application has self development environment.

Import programMbedFileServer

The program publishes files at local directory and SD filesystem. It is a full-fledged webServer somewhat.

サンプル

Import programMiMicSimpleHttpd

This is a simplest HTTP server made ​​of libMiMic. It will echo back a request path.

Import programUPnPBasicDevice

Simplest UPnP basic device example. This program to run UPnP basic device on the mbed.

Import programWebSocketSample

MiMicSDK Websocket module sample program.

Import programHttpClientSamlpe

A http client sample program.

Import programTcpSocketClientSamlpe

MiMicSDK Tcp client socket sample program.

Import programUdpSocketSamlpe

Udp socket sample program. This program will send back the received packet.

チュートリアル

English

libMiMic(MiMic library for mbed) is SDK which provides Webservice functions. It can be created networking device easily using mbed.

See more MiMic information, See MiMic project website. http://nyatla.jp/mimic/wp/

Structure

libMiMic run on mbed RTOS and provides networking API to user application. This library has C++ class API for the mbed, and low-level C language API.

/media/uploads/nyatla/libmimic-sdk.png

For WebService general, it can be written in a simple C + + API.

libMiMic does not have the standard Ethernet driver of mbed. It is not possible that will be used with the standard Ethernet driver.

  • MiMicIP - IPv4 protocol stack. This is based uip which is reduced memory and latency.
  • ARP / ICMP / UDP / TCP - Those are provide basic IP protocols.
  • HTTP/1.1 Server - The Http server compatible HTTP/1.1. It supports multi-session, chunked transport, persistent connection.
  • HTTP Modules - There are addon-module for HTTP server. The following modules.
    • ROM file module - Publish the file images in ROM.
    • File system module - Publish thefiles in mbed file system.
    • Onchip configuration module - To save the network settings to the program flash via REST.
    • MiMicVM module - To handle the (MiMicVM) RPC request.
    • FileUpload module - Accept a file via HTTP POST.
    • URL dedoce module - A versatility URL decoder.
    • UPnP handle module - To handle UPnP messages.
    • UPnP - This provides UPnP/1.0 device functions. It works together with UPnP handler.
    • Websocket - websocket (version13) server
  • mDNS Service - DNS-SD protocol server.
  • UPnP - This provides UPnP/1.0 device functions which works with UPnP handler. (You have been implemented (SSDP) ? description only (Description) device search now.) It is a module zero configuration for - DHCP / APIPA. mbed C + + class library - C of mbed for + + is the CPI. It is simple than that of the C language.
  • DHCP/APIPA - It support zero-cpnfigulation.
  • mbed C++ class library. Almost APIs for Web applications are available.
  • HTTP/1.1 Client

Supported target

  • mbed(mbed LPC1768)
  • LPCXpresso1769

Application

Import programMiMicRemoteMCU-for-Mbed

MiMic RemoteMCU for mbed. This program provides MCU control API over REST API. It can control MCU from Javascript,PHP or any HTTP rest client directly. And, The application has self development environment.

Import programMbedFileServer

The program publishes files at local directory and SD filesystem. It is a full-fledged webServer somewhat.

Sample

Import programMiMicSimpleHttpd

This is a simplest HTTP server made ​​of libMiMic. It will echo back a request path.

Import programUPnPBasicDevice

Simplest UPnP basic device example. This program to run UPnP basic device on the mbed.

Import programWebSocketSample

MiMicSDK Websocket module sample program.

Import programHttpClientSamlpe

A http client sample program.

Import programTcpSocketClientSamlpe

MiMicSDK Tcp client socket sample program.

Import programUdpSocketSamlpe

Udp socket sample program. This program will send back the received packet.

Tutorial

Committer:
nyatla
Date:
Wed Aug 07 13:53:02 2013 +0000
Revision:
48:00d211aac2ec
Parent:
43:a182f2b5ff41
Child:
57:bc4330dfa62f
backup

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nyatla 41:2389bd6b6f74 1 /*********************************************************************************
nyatla 41:2389bd6b6f74 2 * PROJECT: MiMic
nyatla 41:2389bd6b6f74 3 * --------------------------------------------------------------------------------
nyatla 41:2389bd6b6f74 4 *
nyatla 41:2389bd6b6f74 5 * This file is part of MiMic
nyatla 41:2389bd6b6f74 6 * Copyright (C)2011 Ryo Iizuka
nyatla 41:2389bd6b6f74 7 *
nyatla 41:2389bd6b6f74 8 * MiMic is free software: you can redistribute it and/or modify
nyatla 41:2389bd6b6f74 9 * it under the terms of the GNU Lesser General Public License as published
nyatla 41:2389bd6b6f74 10 * by the Free Software Foundation, either version 3 of the License, or
nyatla 41:2389bd6b6f74 11 * (at your option) any later version.
nyatla 41:2389bd6b6f74 12 *
nyatla 41:2389bd6b6f74 13 * This program is distributed in the hope that it will be useful,
nyatla 41:2389bd6b6f74 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nyatla 41:2389bd6b6f74 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nyatla 41:2389bd6b6f74 16 * GNU General Public License for more details.
nyatla 41:2389bd6b6f74 17 *
nyatla 41:2389bd6b6f74 18 * You should have received a copy of the GNU Lesser General Public License
nyatla 41:2389bd6b6f74 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
nyatla 41:2389bd6b6f74 20 *
nyatla 41:2389bd6b6f74 21 * For further information please contact.
nyatla 41:2389bd6b6f74 22 * http://nyatla.jp/
nyatla 41:2389bd6b6f74 23 * <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>
nyatla 41:2389bd6b6f74 24 *
nyatla 41:2389bd6b6f74 25 *********************************************************************************/
nyatla 41:2389bd6b6f74 26 #include "NyLPC_cModFileIoBaseClass.h"
nyatla 41:2389bd6b6f74 27 #include "NyLPC_stdlib.h"
nyatla 41:2389bd6b6f74 28 #include "NyLPC_http.h"
nyatla 41:2389bd6b6f74 29 #include "../NyLPC_cHttpdConnection_protected.h"
nyatla 41:2389bd6b6f74 30 #include "../NyLPC_cHttpdUtils.h"
nyatla 41:2389bd6b6f74 31 #include "NyLPC_net.h"
nyatla 41:2389bd6b6f74 32
nyatla 41:2389bd6b6f74 33 #define MVM_VERSION "ModFileIo/1.0;Json/1.0"
nyatla 41:2389bd6b6f74 34
nyatla 41:2389bd6b6f74 35 #define FNAME_MAX 48
nyatla 41:2389bd6b6f74 36 #define STRBUF_MAX 48
nyatla 41:2389bd6b6f74 37
nyatla 41:2389bd6b6f74 38 struct TModFileIoHeader
nyatla 41:2389bd6b6f74 39 {
nyatla 41:2389bd6b6f74 40 struct NyLPC_THttpBasicHeader super;
nyatla 42:f83aa17dcae5 41 NyLPC_TcUrlEncode_t urlencode;
nyatla 41:2389bd6b6f74 42 NyLPC_TUInt8 _content_id;
nyatla 41:2389bd6b6f74 43 //解析用
nyatla 41:2389bd6b6f74 44 NyLPC_TUInt8 _qery_name_id;
nyatla 41:2389bd6b6f74 45 NyLPC_TUInt8 _astate;
nyatla 41:2389bd6b6f74 46 NyLPC_TInt16 _prefix_len;
nyatla 41:2389bd6b6f74 47 NyLPC_TcStr_t _tstr;
nyatla 41:2389bd6b6f74 48 NyLPC_TChar _tstr_buf[STRBUF_MAX];
nyatla 41:2389bd6b6f74 49 /** 文字列のパーサ*/
nyatla 41:2389bd6b6f74 50 char fname[FNAME_MAX];//対象ファイル名の格納先
nyatla 41:2389bd6b6f74 51 };
nyatla 41:2389bd6b6f74 52
nyatla 41:2389bd6b6f74 53 #define ST_PARSE_PATH 1
nyatla 41:2389bd6b6f74 54 #define ST_PARSE_QUERY_NAME 2
nyatla 41:2389bd6b6f74 55 #define ST_PARSE_QUERY_VALUE 3 //Query読み出し中
nyatla 41:2389bd6b6f74 56 #define ST_PARSE_QUERY_VALUE_NAME 4
nyatla 41:2389bd6b6f74 57 /**
nyatla 41:2389bd6b6f74 58 * コンテンツID定義(コンテンツ名に対応)
nyatla 41:2389bd6b6f74 59 */
nyatla 41:2389bd6b6f74 60 #define CONTENT_ID_UPLOAD 2
nyatla 41:2389bd6b6f74 61 #define CONTENT_ID_CREATE 3
nyatla 41:2389bd6b6f74 62 #define CONTENT_ID_REMOVE 4
nyatla 41:2389bd6b6f74 63 #define CONTENT_ID_UNKNOWN 0
nyatla 41:2389bd6b6f74 64
nyatla 41:2389bd6b6f74 65
nyatla 41:2389bd6b6f74 66 #define QNAME_ID_NAME 1
nyatla 41:2389bd6b6f74 67 #define QNAME_ID_UNKNOWN 0
nyatla 41:2389bd6b6f74 68
nyatla 41:2389bd6b6f74 69
nyatla 41:2389bd6b6f74 70 static const struct NyLPC_TTextIdTbl url_tbl[]=
nyatla 41:2389bd6b6f74 71 {
nyatla 41:2389bd6b6f74 72 {"upload.api",CONTENT_ID_UPLOAD},
nyatla 41:2389bd6b6f74 73 {"create.api",CONTENT_ID_CREATE},
nyatla 41:2389bd6b6f74 74 {"remove.api",CONTENT_ID_REMOVE},
nyatla 41:2389bd6b6f74 75 {NULL,CONTENT_ID_UNKNOWN}
nyatla 41:2389bd6b6f74 76 };
nyatla 41:2389bd6b6f74 77
nyatla 41:2389bd6b6f74 78 static const struct NyLPC_TTextIdTbl qname_id_table[]=
nyatla 41:2389bd6b6f74 79 {
nyatla 41:2389bd6b6f74 80 {"name",QNAME_ID_NAME},
nyatla 41:2389bd6b6f74 81 {NULL,QNAME_ID_UNKNOWN}
nyatla 41:2389bd6b6f74 82 };
nyatla 41:2389bd6b6f74 83
nyatla 41:2389bd6b6f74 84
nyatla 41:2389bd6b6f74 85
nyatla 41:2389bd6b6f74 86 static NyLPC_TBool urlHandler(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TChar i_c,struct NyLPC_THttpBasicHeader* o_out)
nyatla 41:2389bd6b6f74 87 {
nyatla 41:2389bd6b6f74 88 struct TModFileIoHeader* out=(struct TModFileIoHeader*)o_out;
nyatla 42:f83aa17dcae5 89 NyLPC_TChar c;
nyatla 41:2389bd6b6f74 90 //読み飛ばし
nyatla 41:2389bd6b6f74 91 if(out->_prefix_len<0){
nyatla 41:2389bd6b6f74 92 out->_prefix_len++;
nyatla 41:2389bd6b6f74 93 return NyLPC_TBool_TRUE;//読み飛ばし
nyatla 41:2389bd6b6f74 94 }
nyatla 41:2389bd6b6f74 95 //Path解析
nyatla 41:2389bd6b6f74 96 if(out->_astate==ST_PARSE_PATH){
nyatla 41:2389bd6b6f74 97 if(i_c!='\0' && i_c!='?'){
nyatla 41:2389bd6b6f74 98 if(!NyLPC_cStr_put(&(out->_tstr),i_c)){
nyatla 41:2389bd6b6f74 99 //ERROR
nyatla 41:2389bd6b6f74 100 NyLPC_OnErrorGoto(ERROR);
nyatla 41:2389bd6b6f74 101 }
nyatla 41:2389bd6b6f74 102 }else{
nyatla 41:2389bd6b6f74 103 out->_content_id=NyLPC_TTextIdTbl_getMatchId(NyLPC_cStr_str(&(out->_tstr)),url_tbl);
nyatla 41:2389bd6b6f74 104 switch(out->_content_id)
nyatla 41:2389bd6b6f74 105 {
nyatla 41:2389bd6b6f74 106 case CONTENT_ID_UPLOAD:
nyatla 41:2389bd6b6f74 107 case CONTENT_ID_CREATE:
nyatla 41:2389bd6b6f74 108 case CONTENT_ID_REMOVE:
nyatla 41:2389bd6b6f74 109 break;
nyatla 41:2389bd6b6f74 110 default:
nyatla 41:2389bd6b6f74 111 NyLPC_OnErrorGoto(ERROR);
nyatla 41:2389bd6b6f74 112 }
nyatla 41:2389bd6b6f74 113 NyLPC_cStr_clear(&(out->_tstr));
nyatla 41:2389bd6b6f74 114 out->_astate=ST_PARSE_QUERY_NAME;//クエリ名解析へ
nyatla 41:2389bd6b6f74 115 }
nyatla 41:2389bd6b6f74 116 return NyLPC_TBool_TRUE;
nyatla 41:2389bd6b6f74 117 }
nyatla 41:2389bd6b6f74 118 switch(out->_content_id)
nyatla 41:2389bd6b6f74 119 {
nyatla 41:2389bd6b6f74 120 case CONTENT_ID_UPLOAD:
nyatla 41:2389bd6b6f74 121 case CONTENT_ID_CREATE:
nyatla 41:2389bd6b6f74 122 case CONTENT_ID_REMOVE:
nyatla 41:2389bd6b6f74 123 switch(out->_astate){
nyatla 41:2389bd6b6f74 124 case ST_PARSE_QUERY_NAME:
nyatla 41:2389bd6b6f74 125 if(i_c!='\0' && i_c!='&' && i_c!='='){
nyatla 41:2389bd6b6f74 126 if(!NyLPC_cStr_put(&(out->_tstr),i_c)){
nyatla 41:2389bd6b6f74 127 NyLPC_OnErrorGoto(ERROR);
nyatla 41:2389bd6b6f74 128 }
nyatla 41:2389bd6b6f74 129 }else{
nyatla 41:2389bd6b6f74 130 //Query確定。
nyatla 41:2389bd6b6f74 131 out->_qery_name_id=NyLPC_TTextIdTbl_getMatchId(NyLPC_cStr_str(&(out->_tstr)),qname_id_table);
nyatla 41:2389bd6b6f74 132 NyLPC_cStr_clear(&(out->_tstr));
nyatla 41:2389bd6b6f74 133 //クエリ値がある場合
nyatla 41:2389bd6b6f74 134 switch(out->_qery_name_id){
nyatla 41:2389bd6b6f74 135 case QNAME_ID_NAME:
nyatla 42:f83aa17dcae5 136 NyLPC_cUrlEncode_reset(&out->urlencode);
nyatla 41:2389bd6b6f74 137 out->_astate=ST_PARSE_QUERY_VALUE_NAME;
nyatla 41:2389bd6b6f74 138 break;
nyatla 41:2389bd6b6f74 139 default:
nyatla 41:2389bd6b6f74 140 out->_astate=ST_PARSE_QUERY_VALUE;
nyatla 41:2389bd6b6f74 141 break;
nyatla 41:2389bd6b6f74 142 }
nyatla 41:2389bd6b6f74 143 }
nyatla 41:2389bd6b6f74 144 return NyLPC_TBool_TRUE;
nyatla 41:2389bd6b6f74 145 case ST_PARSE_QUERY_VALUE:
nyatla 41:2389bd6b6f74 146 //未知のクエリは無視
nyatla 41:2389bd6b6f74 147 if(i_c!='\0' && i_c!='&'){
nyatla 41:2389bd6b6f74 148 }else{
nyatla 41:2389bd6b6f74 149 //クエリ値解析完了
nyatla 41:2389bd6b6f74 150 out->_astate=ST_PARSE_QUERY_NAME;
nyatla 41:2389bd6b6f74 151 }
nyatla 41:2389bd6b6f74 152 return NyLPC_TBool_TRUE;
nyatla 41:2389bd6b6f74 153 case ST_PARSE_QUERY_VALUE_NAME:
nyatla 41:2389bd6b6f74 154 if(i_c!='\0' && i_c!='&'){
nyatla 42:f83aa17dcae5 155 //URLデコードしながら蓄積
nyatla 42:f83aa17dcae5 156 switch(NyLPC_cUrlEncode_decode(&out->urlencode,i_c,&c)){
nyatla 42:f83aa17dcae5 157 case NyLPC_TcUrlEncode_ST_NEXT:
nyatla 42:f83aa17dcae5 158 break;
nyatla 42:f83aa17dcae5 159 case NyLPC_TcUrlEncode_ST_DONE:
nyatla 42:f83aa17dcae5 160 if(!NyLPC_cStr_put(&(out->_tstr),c)){
nyatla 42:f83aa17dcae5 161 NyLPC_OnErrorGoto(ERROR);
nyatla 42:f83aa17dcae5 162 }
nyatla 42:f83aa17dcae5 163 break;
nyatla 42:f83aa17dcae5 164 default:
nyatla 41:2389bd6b6f74 165 NyLPC_OnErrorGoto(ERROR);
nyatla 41:2389bd6b6f74 166 }
nyatla 41:2389bd6b6f74 167 return NyLPC_TBool_TRUE;
nyatla 41:2389bd6b6f74 168 }else{
nyatla 41:2389bd6b6f74 169 if(NyLPC_cStr_len(&out->_tstr)<1){
nyatla 41:2389bd6b6f74 170 //ファイル名短すぎ
nyatla 41:2389bd6b6f74 171 NyLPC_OnErrorGoto(ERROR);
nyatla 41:2389bd6b6f74 172 }
nyatla 41:2389bd6b6f74 173 //ファイル名を保存
nyatla 41:2389bd6b6f74 174 strcpy(out->fname,(const char*)NyLPC_cStr_str(&out->_tstr));
nyatla 41:2389bd6b6f74 175 //終端しているなら、次のクエリへ
nyatla 41:2389bd6b6f74 176 out->_astate=ST_PARSE_QUERY_NAME;
nyatla 41:2389bd6b6f74 177 }
nyatla 41:2389bd6b6f74 178 return NyLPC_TBool_TRUE;
nyatla 41:2389bd6b6f74 179 default:
nyatla 41:2389bd6b6f74 180 break;
nyatla 41:2389bd6b6f74 181 }
nyatla 41:2389bd6b6f74 182 NyLPC_OnErrorGoto(ERROR);
nyatla 41:2389bd6b6f74 183 default:
nyatla 41:2389bd6b6f74 184 NyLPC_OnErrorGoto(ERROR);
nyatla 41:2389bd6b6f74 185 }
nyatla 41:2389bd6b6f74 186 return NyLPC_TBool_TRUE;
nyatla 41:2389bd6b6f74 187 ERROR:
nyatla 41:2389bd6b6f74 188 return NyLPC_TBool_FALSE;
nyatla 41:2389bd6b6f74 189 }
nyatla 41:2389bd6b6f74 190 /**
nyatla 41:2389bd6b6f74 191 * デフォルトハンドラ
nyatla 41:2389bd6b6f74 192 */
nyatla 41:2389bd6b6f74 193 static const struct NyLPC_TcHttpBasicHeaderParser_Handler handler=
nyatla 41:2389bd6b6f74 194 {
nyatla 48:00d211aac2ec 195 NULL,
nyatla 41:2389bd6b6f74 196 urlHandler
nyatla 41:2389bd6b6f74 197 };
nyatla 41:2389bd6b6f74 198
nyatla 41:2389bd6b6f74 199
nyatla 41:2389bd6b6f74 200 /**
nyatla 41:2389bd6b6f74 201 * コンストラクタ。
nyatla 41:2389bd6b6f74 202 */
nyatla 41:2389bd6b6f74 203 void NyLPC_cModFileIoBaseClass_initialize(NyLPC_TcModFileIoBaseClass_t* i_inst,const NyLPC_TChar* i_ref_root_path)
nyatla 41:2389bd6b6f74 204 {
nyatla 41:2389bd6b6f74 205 NyLPC_cModRomFiles_initialize(&i_inst->super,i_ref_root_path,NULL,0);
nyatla 41:2389bd6b6f74 206 }
nyatla 41:2389bd6b6f74 207 void NyLPC_cModFileIoBaseClass_finalize(NyLPC_TcModFileIoBaseClass_t* i_inst)
nyatla 41:2389bd6b6f74 208 {
nyatla 41:2389bd6b6f74 209 NyLPC_cModRomFiles_finalize(&i_inst->super);
nyatla 41:2389bd6b6f74 210 }
nyatla 41:2389bd6b6f74 211 /**
nyatla 41:2389bd6b6f74 212 * モジュールがコネクションをハンドリングできるかを返します。
nyatla 41:2389bd6b6f74 213 */
nyatla 41:2389bd6b6f74 214 NyLPC_TBool NyLPC_cModFileIoBaseClass_canHandle(NyLPC_TcModFileIoBaseClass_t* i_inst,NyLPC_TcHttpdConnection_t* i_connection)
nyatla 41:2389bd6b6f74 215 {
nyatla 41:2389bd6b6f74 216 return NyLPC_cModRomFiles_canHandle(&i_inst->super,i_connection);
nyatla 41:2389bd6b6f74 217 }
nyatla 41:2389bd6b6f74 218
nyatla 41:2389bd6b6f74 219 static struct TModFileIoHeader single_header;
nyatla 41:2389bd6b6f74 220
nyatla 41:2389bd6b6f74 221
nyatla 41:2389bd6b6f74 222 /**
nyatla 41:2389bd6b6f74 223 * モジュールを実行します。
nyatla 41:2389bd6b6f74 224 */
nyatla 41:2389bd6b6f74 225 NyLPC_TBool NyLPC_cModFileIoBaseClass_execute(NyLPC_TcModFileIoBaseClass_t* i_inst,NyLPC_TcHttpdConnection_t* i_connection)
nyatla 41:2389bd6b6f74 226 {
nyatla 41:2389bd6b6f74 227 union{
nyatla 41:2389bd6b6f74 228 NyLPC_TcHttpBasicHeaderParser_t parser;
nyatla 41:2389bd6b6f74 229 NyLPC_TcHttpBodyParser_t body_parser;
nyatla 41:2389bd6b6f74 230 }sh;
nyatla 41:2389bd6b6f74 231 NyLPC_TUInt8 method_type;
nyatla 41:2389bd6b6f74 232 //リクエストParse済へ遷移(この関数の後はModが責任を持ってリクエストを返却)
nyatla 41:2389bd6b6f74 233 NyLPC_cHttpdConnection_setReqStatusParsed(i_connection);
nyatla 41:2389bd6b6f74 234
nyatla 41:2389bd6b6f74 235 //排他ロック
nyatla 41:2389bd6b6f74 236 NyLPC_cHttpdConnection_lock(i_connection);
nyatla 41:2389bd6b6f74 237 {//parser
nyatla 41:2389bd6b6f74 238
nyatla 41:2389bd6b6f74 239 //URL解析の準備
nyatla 41:2389bd6b6f74 240 single_header._prefix_len=-((NyLPC_TInt16)strlen(i_inst->super._ref_root_path)+2);
nyatla 41:2389bd6b6f74 241 single_header._astate=ST_PARSE_PATH;
nyatla 41:2389bd6b6f74 242 single_header.fname[0]='\0';
nyatla 42:f83aa17dcae5 243 NyLPC_cUrlEncode_initialize(&single_header.urlencode);
nyatla 41:2389bd6b6f74 244 NyLPC_cStr_initialize(&single_header._tstr,single_header._tstr_buf,STRBUF_MAX);
nyatla 41:2389bd6b6f74 245
nyatla 41:2389bd6b6f74 246 NyLPC_cHttpBasicHeaderParser_initialize(&sh.parser,&handler);
nyatla 41:2389bd6b6f74 247
nyatla 41:2389bd6b6f74 248 //プリフェッチしたデータを流す
nyatla 41:2389bd6b6f74 249 NyLPC_cHttpBasicHeaderParser_parseInit(&sh.parser,&(single_header.super));
nyatla 41:2389bd6b6f74 250 NyLPC_cHttpdConnection_pushPrefetchInfo(i_connection,&sh.parser,&single_header.super);
nyatla 41:2389bd6b6f74 251 //後続をストリームから取り込む
nyatla 41:2389bd6b6f74 252 if(!NyLPC_cHttpBasicHeaderParser_parseStream(&sh.parser,NyLPC_cHttpdConnection_refStream(i_connection),&(single_header.super))){
nyatla 41:2389bd6b6f74 253 NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500);
nyatla 41:2389bd6b6f74 254 NyLPC_OnErrorGoto(Error1);
nyatla 41:2389bd6b6f74 255 }
nyatla 41:2389bd6b6f74 256 if(!NyLPC_cHttpBasicHeaderParser_parseFinish(&sh.parser,&(single_header.super))){
nyatla 41:2389bd6b6f74 257 NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500);
nyatla 41:2389bd6b6f74 258 NyLPC_OnErrorGoto(Error1);
nyatla 41:2389bd6b6f74 259 }
nyatla 42:f83aa17dcae5 260 //HeaderParserはここで破棄(URLEncode,cSTRも)
nyatla 41:2389bd6b6f74 261 NyLPC_cHttpBasicHeaderParser_finalize(&sh.parser);
nyatla 42:f83aa17dcae5 262 NyLPC_cUrlEncode_finalize(&single_header.urlencode);
nyatla 42:f83aa17dcae5 263 NyLPC_cStr_finalize(&single_header._tstr);
nyatla 41:2389bd6b6f74 264 }
nyatla 43:a182f2b5ff41 265 //Request::ConnectionがClose設定,又はHTTP1.1では無い場合,CLOSE
nyatla 43:a182f2b5ff41 266 if(single_header.super.connection==NyLPC_THttpMessgeHeader_Connection_CLOSE || single_header.super.startline.req.version!=NyLPC_THttpVersion_11)
nyatla 41:2389bd6b6f74 267 {
nyatla 43:a182f2b5ff41 268 NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE);
nyatla 41:2389bd6b6f74 269 }
nyatla 41:2389bd6b6f74 270 //返答フェーズ
nyatla 41:2389bd6b6f74 271 {
nyatla 41:2389bd6b6f74 272 method_type=NyLPC_cHttpdConnection_getMethod(i_connection);
nyatla 41:2389bd6b6f74 273
nyatla 41:2389bd6b6f74 274 //CGIの実行
nyatla 41:2389bd6b6f74 275 switch(single_header._content_id)
nyatla 41:2389bd6b6f74 276 {
nyatla 41:2389bd6b6f74 277 case CONTENT_ID_UPLOAD:
nyatla 41:2389bd6b6f74 278 //ファイル名とBodyParserを通知
nyatla 42:f83aa17dcae5 279 if(method_type==NyLPC_THttpMethodType_POST)
nyatla 41:2389bd6b6f74 280 {
nyatla 42:f83aa17dcae5 281 NyLPC_cHttpdConnection_send100Continue(i_connection);
nyatla 41:2389bd6b6f74 282 NyLPC_cHttpBodyParser_initialize(&sh.body_parser);
nyatla 41:2389bd6b6f74 283 NyLPC_cHttpBodyParser_parseInit(&sh.body_parser,&single_header.super);
nyatla 41:2389bd6b6f74 284 //ハンドラ内ではparseStreamのみ実行
nyatla 41:2389bd6b6f74 285 if(!i_inst->_abstruct_function.upload(i_connection,single_header.fname,&sh.body_parser)){
nyatla 41:2389bd6b6f74 286 NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE);
nyatla 41:2389bd6b6f74 287 }
nyatla 41:2389bd6b6f74 288 if(NyLPC_cHttpBodyParser_parseFinish(&sh.body_parser)){
nyatla 41:2389bd6b6f74 289 NyLPC_cHttpBodyParser_finalize(&sh.body_parser);
nyatla 41:2389bd6b6f74 290 NyLPC_OnErrorGoto(Error2);//エラーメッセージはハンドラ内で送られていると仮定する。
nyatla 41:2389bd6b6f74 291 }
nyatla 41:2389bd6b6f74 292 NyLPC_cHttpBodyParser_finalize(&sh.body_parser);
nyatla 41:2389bd6b6f74 293 }else{
nyatla 41:2389bd6b6f74 294 NyLPC_OnErrorGoto(Error2);
nyatla 41:2389bd6b6f74 295 }
nyatla 41:2389bd6b6f74 296 break;
nyatla 41:2389bd6b6f74 297 case CONTENT_ID_CREATE:
nyatla 41:2389bd6b6f74 298 if(method_type==NyLPC_THttpMethodType_GET || method_type==NyLPC_THttpMethodType_HEAD)
nyatla 41:2389bd6b6f74 299 {
nyatla 41:2389bd6b6f74 300 //イベント起動
nyatla 41:2389bd6b6f74 301 if(!i_inst->_abstruct_function.create(i_connection,single_header.fname)){
nyatla 41:2389bd6b6f74 302 NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE);
nyatla 41:2389bd6b6f74 303 }
nyatla 41:2389bd6b6f74 304 break;
nyatla 41:2389bd6b6f74 305 }
nyatla 41:2389bd6b6f74 306 NyLPC_OnErrorGoto(Error2_405);
nyatla 41:2389bd6b6f74 307 case CONTENT_ID_REMOVE:
nyatla 41:2389bd6b6f74 308 //ファイル名を通知
nyatla 41:2389bd6b6f74 309 if(method_type==NyLPC_THttpMethodType_GET || method_type==NyLPC_THttpMethodType_HEAD)
nyatla 41:2389bd6b6f74 310 {
nyatla 41:2389bd6b6f74 311 //イベント起動
nyatla 41:2389bd6b6f74 312 if(!i_inst->_abstruct_function.remove(i_connection,single_header.fname)){
nyatla 41:2389bd6b6f74 313 NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE);
nyatla 41:2389bd6b6f74 314 }
nyatla 41:2389bd6b6f74 315 break;
nyatla 41:2389bd6b6f74 316 }
nyatla 41:2389bd6b6f74 317 NyLPC_OnErrorGoto(Error2_405);
nyatla 41:2389bd6b6f74 318 default:
nyatla 41:2389bd6b6f74 319 NyLPC_cHttpdUtils_sendErrorResponse(i_connection,400);
nyatla 41:2389bd6b6f74 320 NyLPC_OnErrorGoto(Error2);
nyatla 41:2389bd6b6f74 321 }
nyatla 41:2389bd6b6f74 322 }
nyatla 41:2389bd6b6f74 323 //占有解除
nyatla 41:2389bd6b6f74 324 NyLPC_cHttpdConnection_unlock(i_connection);
nyatla 41:2389bd6b6f74 325 return NyLPC_TBool_TRUE;
nyatla 41:2389bd6b6f74 326 Error2_405:
nyatla 41:2389bd6b6f74 327 NyLPC_cHttpdUtils_sendErrorResponse(i_connection,405);
nyatla 41:2389bd6b6f74 328 Error2:
nyatla 41:2389bd6b6f74 329 //VM排他ロックの解除
nyatla 41:2389bd6b6f74 330 NyLPC_cHttpdConnection_unlock(i_connection);
nyatla 41:2389bd6b6f74 331 return NyLPC_TBool_FALSE;
nyatla 41:2389bd6b6f74 332 Error1:
nyatla 41:2389bd6b6f74 333 NyLPC_cHttpBasicHeaderParser_finalize(&parser);
nyatla 41:2389bd6b6f74 334 NyLPC_cStr_finalize(&single_header._tstr);
nyatla 42:f83aa17dcae5 335 NyLPC_cUrlEncode_finalize(&single_header.urlencode);
nyatla 41:2389bd6b6f74 336 //VM排他ロックの解除
nyatla 41:2389bd6b6f74 337 NyLPC_cHttpdConnection_unlock(i_connection);
nyatla 41:2389bd6b6f74 338 return NyLPC_TBool_FALSE;
nyatla 41:2389bd6b6f74 339 }
nyatla 41:2389bd6b6f74 340
nyatla 41:2389bd6b6f74 341