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:
Tue Jul 09 12:07:17 2013 +0000
Revision:
43:a182f2b5ff41
Parent:
42:f83aa17dcae5
Child:
48:00d211aac2ec
update to r276;

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
nyatla 41:2389bd6b6f74 54
nyatla 42:f83aa17dcae5 55
nyatla 41:2389bd6b6f74 56 static NyLPC_TBool messageHandler(NyLPC_TcHttpBasicHeaderParser_t* i_inst,const NyLPC_TChar* i_name,NyLPC_TChar i_c,struct NyLPC_THttpBasicHeader* o_out)
nyatla 41:2389bd6b6f74 57 {
nyatla 41:2389bd6b6f74 58 (void)i_inst;
nyatla 41:2389bd6b6f74 59 (void)i_name;
nyatla 41:2389bd6b6f74 60 (void)i_c;
nyatla 41:2389bd6b6f74 61 return NyLPC_TBool_TRUE;
nyatla 41:2389bd6b6f74 62 }
nyatla 41:2389bd6b6f74 63
nyatla 41:2389bd6b6f74 64 #define ST_PARSE_PATH 1
nyatla 41:2389bd6b6f74 65 #define ST_PARSE_QUERY_NAME 2
nyatla 41:2389bd6b6f74 66 #define ST_PARSE_QUERY_VALUE 3 //Query読み出し中
nyatla 41:2389bd6b6f74 67 #define ST_PARSE_QUERY_VALUE_NAME 4
nyatla 41:2389bd6b6f74 68 /**
nyatla 41:2389bd6b6f74 69 * コンテンツID定義(コンテンツ名に対応)
nyatla 41:2389bd6b6f74 70 */
nyatla 41:2389bd6b6f74 71 #define CONTENT_ID_UPLOAD 2
nyatla 41:2389bd6b6f74 72 #define CONTENT_ID_CREATE 3
nyatla 41:2389bd6b6f74 73 #define CONTENT_ID_REMOVE 4
nyatla 41:2389bd6b6f74 74 #define CONTENT_ID_UNKNOWN 0
nyatla 41:2389bd6b6f74 75
nyatla 41:2389bd6b6f74 76
nyatla 41:2389bd6b6f74 77 #define QNAME_ID_NAME 1
nyatla 41:2389bd6b6f74 78 #define QNAME_ID_UNKNOWN 0
nyatla 41:2389bd6b6f74 79
nyatla 41:2389bd6b6f74 80
nyatla 41:2389bd6b6f74 81 static const struct NyLPC_TTextIdTbl url_tbl[]=
nyatla 41:2389bd6b6f74 82 {
nyatla 41:2389bd6b6f74 83 {"upload.api",CONTENT_ID_UPLOAD},
nyatla 41:2389bd6b6f74 84 {"create.api",CONTENT_ID_CREATE},
nyatla 41:2389bd6b6f74 85 {"remove.api",CONTENT_ID_REMOVE},
nyatla 41:2389bd6b6f74 86 {NULL,CONTENT_ID_UNKNOWN}
nyatla 41:2389bd6b6f74 87 };
nyatla 41:2389bd6b6f74 88
nyatla 41:2389bd6b6f74 89 static const struct NyLPC_TTextIdTbl qname_id_table[]=
nyatla 41:2389bd6b6f74 90 {
nyatla 41:2389bd6b6f74 91 {"name",QNAME_ID_NAME},
nyatla 41:2389bd6b6f74 92 {NULL,QNAME_ID_UNKNOWN}
nyatla 41:2389bd6b6f74 93 };
nyatla 41:2389bd6b6f74 94
nyatla 41:2389bd6b6f74 95
nyatla 41:2389bd6b6f74 96
nyatla 41:2389bd6b6f74 97 static NyLPC_TBool urlHandler(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TChar i_c,struct NyLPC_THttpBasicHeader* o_out)
nyatla 41:2389bd6b6f74 98 {
nyatla 41:2389bd6b6f74 99 struct TModFileIoHeader* out=(struct TModFileIoHeader*)o_out;
nyatla 42:f83aa17dcae5 100 NyLPC_TChar c;
nyatla 41:2389bd6b6f74 101 //読み飛ばし
nyatla 41:2389bd6b6f74 102 if(out->_prefix_len<0){
nyatla 41:2389bd6b6f74 103 out->_prefix_len++;
nyatla 41:2389bd6b6f74 104 return NyLPC_TBool_TRUE;//読み飛ばし
nyatla 41:2389bd6b6f74 105 }
nyatla 41:2389bd6b6f74 106 //Path解析
nyatla 41:2389bd6b6f74 107 if(out->_astate==ST_PARSE_PATH){
nyatla 41:2389bd6b6f74 108 if(i_c!='\0' && i_c!='?'){
nyatla 41:2389bd6b6f74 109 if(!NyLPC_cStr_put(&(out->_tstr),i_c)){
nyatla 41:2389bd6b6f74 110 //ERROR
nyatla 41:2389bd6b6f74 111 NyLPC_OnErrorGoto(ERROR);
nyatla 41:2389bd6b6f74 112 }
nyatla 41:2389bd6b6f74 113 }else{
nyatla 41:2389bd6b6f74 114 out->_content_id=NyLPC_TTextIdTbl_getMatchId(NyLPC_cStr_str(&(out->_tstr)),url_tbl);
nyatla 41:2389bd6b6f74 115 switch(out->_content_id)
nyatla 41:2389bd6b6f74 116 {
nyatla 41:2389bd6b6f74 117 case CONTENT_ID_UPLOAD:
nyatla 41:2389bd6b6f74 118 case CONTENT_ID_CREATE:
nyatla 41:2389bd6b6f74 119 case CONTENT_ID_REMOVE:
nyatla 41:2389bd6b6f74 120 break;
nyatla 41:2389bd6b6f74 121 default:
nyatla 41:2389bd6b6f74 122 NyLPC_OnErrorGoto(ERROR);
nyatla 41:2389bd6b6f74 123 }
nyatla 41:2389bd6b6f74 124 NyLPC_cStr_clear(&(out->_tstr));
nyatla 41:2389bd6b6f74 125 out->_astate=ST_PARSE_QUERY_NAME;//クエリ名解析へ
nyatla 41:2389bd6b6f74 126 }
nyatla 41:2389bd6b6f74 127 return NyLPC_TBool_TRUE;
nyatla 41:2389bd6b6f74 128 }
nyatla 41:2389bd6b6f74 129 switch(out->_content_id)
nyatla 41:2389bd6b6f74 130 {
nyatla 41:2389bd6b6f74 131 case CONTENT_ID_UPLOAD:
nyatla 41:2389bd6b6f74 132 case CONTENT_ID_CREATE:
nyatla 41:2389bd6b6f74 133 case CONTENT_ID_REMOVE:
nyatla 41:2389bd6b6f74 134 switch(out->_astate){
nyatla 41:2389bd6b6f74 135 case ST_PARSE_QUERY_NAME:
nyatla 41:2389bd6b6f74 136 if(i_c!='\0' && i_c!='&' && i_c!='='){
nyatla 41:2389bd6b6f74 137 if(!NyLPC_cStr_put(&(out->_tstr),i_c)){
nyatla 41:2389bd6b6f74 138 NyLPC_OnErrorGoto(ERROR);
nyatla 41:2389bd6b6f74 139 }
nyatla 41:2389bd6b6f74 140 }else{
nyatla 41:2389bd6b6f74 141 //Query確定。
nyatla 41:2389bd6b6f74 142 out->_qery_name_id=NyLPC_TTextIdTbl_getMatchId(NyLPC_cStr_str(&(out->_tstr)),qname_id_table);
nyatla 41:2389bd6b6f74 143 NyLPC_cStr_clear(&(out->_tstr));
nyatla 41:2389bd6b6f74 144 //クエリ値がある場合
nyatla 41:2389bd6b6f74 145 switch(out->_qery_name_id){
nyatla 41:2389bd6b6f74 146 case QNAME_ID_NAME:
nyatla 42:f83aa17dcae5 147 NyLPC_cUrlEncode_reset(&out->urlencode);
nyatla 41:2389bd6b6f74 148 out->_astate=ST_PARSE_QUERY_VALUE_NAME;
nyatla 41:2389bd6b6f74 149 break;
nyatla 41:2389bd6b6f74 150 default:
nyatla 41:2389bd6b6f74 151 out->_astate=ST_PARSE_QUERY_VALUE;
nyatla 41:2389bd6b6f74 152 break;
nyatla 41:2389bd6b6f74 153 }
nyatla 41:2389bd6b6f74 154 }
nyatla 41:2389bd6b6f74 155 return NyLPC_TBool_TRUE;
nyatla 41:2389bd6b6f74 156 case ST_PARSE_QUERY_VALUE:
nyatla 41:2389bd6b6f74 157 //未知のクエリは無視
nyatla 41:2389bd6b6f74 158 if(i_c!='\0' && i_c!='&'){
nyatla 41:2389bd6b6f74 159 }else{
nyatla 41:2389bd6b6f74 160 //クエリ値解析完了
nyatla 41:2389bd6b6f74 161 out->_astate=ST_PARSE_QUERY_NAME;
nyatla 41:2389bd6b6f74 162 }
nyatla 41:2389bd6b6f74 163 return NyLPC_TBool_TRUE;
nyatla 41:2389bd6b6f74 164 case ST_PARSE_QUERY_VALUE_NAME:
nyatla 41:2389bd6b6f74 165 if(i_c!='\0' && i_c!='&'){
nyatla 42:f83aa17dcae5 166 //URLデコードしながら蓄積
nyatla 42:f83aa17dcae5 167 switch(NyLPC_cUrlEncode_decode(&out->urlencode,i_c,&c)){
nyatla 42:f83aa17dcae5 168 case NyLPC_TcUrlEncode_ST_NEXT:
nyatla 42:f83aa17dcae5 169 break;
nyatla 42:f83aa17dcae5 170 case NyLPC_TcUrlEncode_ST_DONE:
nyatla 42:f83aa17dcae5 171 if(!NyLPC_cStr_put(&(out->_tstr),c)){
nyatla 42:f83aa17dcae5 172 NyLPC_OnErrorGoto(ERROR);
nyatla 42:f83aa17dcae5 173 }
nyatla 42:f83aa17dcae5 174 break;
nyatla 42:f83aa17dcae5 175 default:
nyatla 41:2389bd6b6f74 176 NyLPC_OnErrorGoto(ERROR);
nyatla 41:2389bd6b6f74 177 }
nyatla 41:2389bd6b6f74 178 return NyLPC_TBool_TRUE;
nyatla 41:2389bd6b6f74 179 }else{
nyatla 41:2389bd6b6f74 180 if(NyLPC_cStr_len(&out->_tstr)<1){
nyatla 41:2389bd6b6f74 181 //ファイル名短すぎ
nyatla 41:2389bd6b6f74 182 NyLPC_OnErrorGoto(ERROR);
nyatla 41:2389bd6b6f74 183 }
nyatla 41:2389bd6b6f74 184 //ファイル名を保存
nyatla 41:2389bd6b6f74 185 strcpy(out->fname,(const char*)NyLPC_cStr_str(&out->_tstr));
nyatla 41:2389bd6b6f74 186 //終端しているなら、次のクエリへ
nyatla 41:2389bd6b6f74 187 out->_astate=ST_PARSE_QUERY_NAME;
nyatla 41:2389bd6b6f74 188 }
nyatla 41:2389bd6b6f74 189 return NyLPC_TBool_TRUE;
nyatla 41:2389bd6b6f74 190 default:
nyatla 41:2389bd6b6f74 191 break;
nyatla 41:2389bd6b6f74 192 }
nyatla 41:2389bd6b6f74 193 NyLPC_OnErrorGoto(ERROR);
nyatla 41:2389bd6b6f74 194 default:
nyatla 41:2389bd6b6f74 195 NyLPC_OnErrorGoto(ERROR);
nyatla 41:2389bd6b6f74 196 }
nyatla 41:2389bd6b6f74 197 return NyLPC_TBool_TRUE;
nyatla 41:2389bd6b6f74 198 ERROR:
nyatla 41:2389bd6b6f74 199 return NyLPC_TBool_FALSE;
nyatla 41:2389bd6b6f74 200 }
nyatla 41:2389bd6b6f74 201 /**
nyatla 41:2389bd6b6f74 202 * デフォルトハンドラ
nyatla 41:2389bd6b6f74 203 */
nyatla 41:2389bd6b6f74 204 static const struct NyLPC_TcHttpBasicHeaderParser_Handler handler=
nyatla 41:2389bd6b6f74 205 {
nyatla 41:2389bd6b6f74 206 messageHandler,
nyatla 41:2389bd6b6f74 207 urlHandler
nyatla 41:2389bd6b6f74 208 };
nyatla 41:2389bd6b6f74 209
nyatla 41:2389bd6b6f74 210
nyatla 41:2389bd6b6f74 211 /**
nyatla 41:2389bd6b6f74 212 * コンストラクタ。
nyatla 41:2389bd6b6f74 213 */
nyatla 41:2389bd6b6f74 214 void NyLPC_cModFileIoBaseClass_initialize(NyLPC_TcModFileIoBaseClass_t* i_inst,const NyLPC_TChar* i_ref_root_path)
nyatla 41:2389bd6b6f74 215 {
nyatla 41:2389bd6b6f74 216 NyLPC_cModRomFiles_initialize(&i_inst->super,i_ref_root_path,NULL,0);
nyatla 41:2389bd6b6f74 217 }
nyatla 41:2389bd6b6f74 218 void NyLPC_cModFileIoBaseClass_finalize(NyLPC_TcModFileIoBaseClass_t* i_inst)
nyatla 41:2389bd6b6f74 219 {
nyatla 41:2389bd6b6f74 220 NyLPC_cModRomFiles_finalize(&i_inst->super);
nyatla 41:2389bd6b6f74 221 }
nyatla 41:2389bd6b6f74 222 /**
nyatla 41:2389bd6b6f74 223 * モジュールがコネクションをハンドリングできるかを返します。
nyatla 41:2389bd6b6f74 224 */
nyatla 41:2389bd6b6f74 225 NyLPC_TBool NyLPC_cModFileIoBaseClass_canHandle(NyLPC_TcModFileIoBaseClass_t* i_inst,NyLPC_TcHttpdConnection_t* i_connection)
nyatla 41:2389bd6b6f74 226 {
nyatla 41:2389bd6b6f74 227 return NyLPC_cModRomFiles_canHandle(&i_inst->super,i_connection);
nyatla 41:2389bd6b6f74 228 }
nyatla 41:2389bd6b6f74 229
nyatla 41:2389bd6b6f74 230 static struct TModFileIoHeader single_header;
nyatla 41:2389bd6b6f74 231
nyatla 41:2389bd6b6f74 232
nyatla 41:2389bd6b6f74 233 /**
nyatla 41:2389bd6b6f74 234 * モジュールを実行します。
nyatla 41:2389bd6b6f74 235 */
nyatla 41:2389bd6b6f74 236 NyLPC_TBool NyLPC_cModFileIoBaseClass_execute(NyLPC_TcModFileIoBaseClass_t* i_inst,NyLPC_TcHttpdConnection_t* i_connection)
nyatla 41:2389bd6b6f74 237 {
nyatla 41:2389bd6b6f74 238 union{
nyatla 41:2389bd6b6f74 239 NyLPC_TcHttpBasicHeaderParser_t parser;
nyatla 41:2389bd6b6f74 240 NyLPC_TcHttpBodyParser_t body_parser;
nyatla 41:2389bd6b6f74 241 }sh;
nyatla 41:2389bd6b6f74 242 NyLPC_TUInt8 method_type;
nyatla 41:2389bd6b6f74 243 //リクエストParse済へ遷移(この関数の後はModが責任を持ってリクエストを返却)
nyatla 41:2389bd6b6f74 244 NyLPC_cHttpdConnection_setReqStatusParsed(i_connection);
nyatla 41:2389bd6b6f74 245
nyatla 41:2389bd6b6f74 246 //排他ロック
nyatla 41:2389bd6b6f74 247 NyLPC_cHttpdConnection_lock(i_connection);
nyatla 41:2389bd6b6f74 248 {//parser
nyatla 41:2389bd6b6f74 249
nyatla 41:2389bd6b6f74 250 //URL解析の準備
nyatla 41:2389bd6b6f74 251 single_header._prefix_len=-((NyLPC_TInt16)strlen(i_inst->super._ref_root_path)+2);
nyatla 41:2389bd6b6f74 252 single_header._astate=ST_PARSE_PATH;
nyatla 41:2389bd6b6f74 253 single_header.fname[0]='\0';
nyatla 42:f83aa17dcae5 254 NyLPC_cUrlEncode_initialize(&single_header.urlencode);
nyatla 41:2389bd6b6f74 255 NyLPC_cStr_initialize(&single_header._tstr,single_header._tstr_buf,STRBUF_MAX);
nyatla 41:2389bd6b6f74 256
nyatla 41:2389bd6b6f74 257 NyLPC_cHttpBasicHeaderParser_initialize(&sh.parser,&handler);
nyatla 41:2389bd6b6f74 258
nyatla 41:2389bd6b6f74 259 //プリフェッチしたデータを流す
nyatla 41:2389bd6b6f74 260 NyLPC_cHttpBasicHeaderParser_parseInit(&sh.parser,&(single_header.super));
nyatla 41:2389bd6b6f74 261 NyLPC_cHttpdConnection_pushPrefetchInfo(i_connection,&sh.parser,&single_header.super);
nyatla 41:2389bd6b6f74 262 //後続をストリームから取り込む
nyatla 41:2389bd6b6f74 263 if(!NyLPC_cHttpBasicHeaderParser_parseStream(&sh.parser,NyLPC_cHttpdConnection_refStream(i_connection),&(single_header.super))){
nyatla 41:2389bd6b6f74 264 NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500);
nyatla 41:2389bd6b6f74 265 NyLPC_OnErrorGoto(Error1);
nyatla 41:2389bd6b6f74 266 }
nyatla 41:2389bd6b6f74 267 if(!NyLPC_cHttpBasicHeaderParser_parseFinish(&sh.parser,&(single_header.super))){
nyatla 41:2389bd6b6f74 268 NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500);
nyatla 41:2389bd6b6f74 269 NyLPC_OnErrorGoto(Error1);
nyatla 41:2389bd6b6f74 270 }
nyatla 42:f83aa17dcae5 271 //HeaderParserはここで破棄(URLEncode,cSTRも)
nyatla 41:2389bd6b6f74 272 NyLPC_cHttpBasicHeaderParser_finalize(&sh.parser);
nyatla 42:f83aa17dcae5 273 NyLPC_cUrlEncode_finalize(&single_header.urlencode);
nyatla 42:f83aa17dcae5 274 NyLPC_cStr_finalize(&single_header._tstr);
nyatla 41:2389bd6b6f74 275 }
nyatla 43:a182f2b5ff41 276 //Request::ConnectionがClose設定,又はHTTP1.1では無い場合,CLOSE
nyatla 43:a182f2b5ff41 277 if(single_header.super.connection==NyLPC_THttpMessgeHeader_Connection_CLOSE || single_header.super.startline.req.version!=NyLPC_THttpVersion_11)
nyatla 41:2389bd6b6f74 278 {
nyatla 43:a182f2b5ff41 279 NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE);
nyatla 41:2389bd6b6f74 280 }
nyatla 41:2389bd6b6f74 281 //返答フェーズ
nyatla 41:2389bd6b6f74 282 {
nyatla 41:2389bd6b6f74 283 method_type=NyLPC_cHttpdConnection_getMethod(i_connection);
nyatla 41:2389bd6b6f74 284
nyatla 41:2389bd6b6f74 285 //CGIの実行
nyatla 41:2389bd6b6f74 286 switch(single_header._content_id)
nyatla 41:2389bd6b6f74 287 {
nyatla 41:2389bd6b6f74 288 case CONTENT_ID_UPLOAD:
nyatla 41:2389bd6b6f74 289 //ファイル名とBodyParserを通知
nyatla 42:f83aa17dcae5 290 if(method_type==NyLPC_THttpMethodType_POST)
nyatla 41:2389bd6b6f74 291 {
nyatla 42:f83aa17dcae5 292 NyLPC_cHttpdConnection_send100Continue(i_connection);
nyatla 41:2389bd6b6f74 293 NyLPC_cHttpBodyParser_initialize(&sh.body_parser);
nyatla 41:2389bd6b6f74 294 NyLPC_cHttpBodyParser_parseInit(&sh.body_parser,&single_header.super);
nyatla 41:2389bd6b6f74 295 //ハンドラ内ではparseStreamのみ実行
nyatla 41:2389bd6b6f74 296 if(!i_inst->_abstruct_function.upload(i_connection,single_header.fname,&sh.body_parser)){
nyatla 41:2389bd6b6f74 297 NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE);
nyatla 41:2389bd6b6f74 298 }
nyatla 41:2389bd6b6f74 299 if(NyLPC_cHttpBodyParser_parseFinish(&sh.body_parser)){
nyatla 41:2389bd6b6f74 300 NyLPC_cHttpBodyParser_finalize(&sh.body_parser);
nyatla 41:2389bd6b6f74 301 NyLPC_OnErrorGoto(Error2);//エラーメッセージはハンドラ内で送られていると仮定する。
nyatla 41:2389bd6b6f74 302 }
nyatla 41:2389bd6b6f74 303 NyLPC_cHttpBodyParser_finalize(&sh.body_parser);
nyatla 41:2389bd6b6f74 304 }else{
nyatla 41:2389bd6b6f74 305 NyLPC_OnErrorGoto(Error2);
nyatla 41:2389bd6b6f74 306 }
nyatla 41:2389bd6b6f74 307 break;
nyatla 41:2389bd6b6f74 308 case CONTENT_ID_CREATE:
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.create(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 case CONTENT_ID_REMOVE:
nyatla 41:2389bd6b6f74 319 //ファイル名を通知
nyatla 41:2389bd6b6f74 320 if(method_type==NyLPC_THttpMethodType_GET || method_type==NyLPC_THttpMethodType_HEAD)
nyatla 41:2389bd6b6f74 321 {
nyatla 41:2389bd6b6f74 322 //イベント起動
nyatla 41:2389bd6b6f74 323 if(!i_inst->_abstruct_function.remove(i_connection,single_header.fname)){
nyatla 41:2389bd6b6f74 324 NyLPC_cHttpdConnection_setConnectionMode(i_connection,NyLPC_TcHttpdConnection_CONNECTION_MODE_CLOSE);
nyatla 41:2389bd6b6f74 325 }
nyatla 41:2389bd6b6f74 326 break;
nyatla 41:2389bd6b6f74 327 }
nyatla 41:2389bd6b6f74 328 NyLPC_OnErrorGoto(Error2_405);
nyatla 41:2389bd6b6f74 329 default:
nyatla 41:2389bd6b6f74 330 NyLPC_cHttpdUtils_sendErrorResponse(i_connection,400);
nyatla 41:2389bd6b6f74 331 NyLPC_OnErrorGoto(Error2);
nyatla 41:2389bd6b6f74 332 }
nyatla 41:2389bd6b6f74 333 }
nyatla 41:2389bd6b6f74 334 //占有解除
nyatla 41:2389bd6b6f74 335 NyLPC_cHttpdConnection_unlock(i_connection);
nyatla 41:2389bd6b6f74 336 return NyLPC_TBool_TRUE;
nyatla 41:2389bd6b6f74 337 Error2_405:
nyatla 41:2389bd6b6f74 338 NyLPC_cHttpdUtils_sendErrorResponse(i_connection,405);
nyatla 41:2389bd6b6f74 339 Error2:
nyatla 41:2389bd6b6f74 340 //VM排他ロックの解除
nyatla 41:2389bd6b6f74 341 NyLPC_cHttpdConnection_unlock(i_connection);
nyatla 41:2389bd6b6f74 342 return NyLPC_TBool_FALSE;
nyatla 41:2389bd6b6f74 343 Error1:
nyatla 41:2389bd6b6f74 344 NyLPC_cHttpBasicHeaderParser_finalize(&parser);
nyatla 41:2389bd6b6f74 345 NyLPC_cStr_finalize(&single_header._tstr);
nyatla 42:f83aa17dcae5 346 NyLPC_cUrlEncode_finalize(&single_header.urlencode);
nyatla 41:2389bd6b6f74 347 //VM排他ロックの解除
nyatla 41:2389bd6b6f74 348 NyLPC_cHttpdConnection_unlock(i_connection);
nyatla 41:2389bd6b6f74 349 return NyLPC_TBool_FALSE;
nyatla 41:2389bd6b6f74 350 }
nyatla 41:2389bd6b6f74 351
nyatla 41:2389bd6b6f74 352