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:
Mon Mar 16 14:19:16 2015 +0000
Revision:
111:f525c6e5e27b
Parent:
48:00d211aac2ec
ModLocalfilesystem; ; Add virtual directory option.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nyatla 17:f29e1ca90e3d 1 #include "ModLocalFileSystem.h"
nyatla 17:f29e1ca90e3d 2 #include "HttpdConnection.h"
nyatla 18:1970fec78229 3 #include "UrlReader.h"
nyatla 18:1970fec78229 4 #include "Http.h"
nyatla 19:33b9ba0859ee 5 #include "Httpd.h"
nyatla 17:f29e1ca90e3d 6 #include "NyLPC_net.h"
nyatla 17:f29e1ca90e3d 7 #include <stdio.h>
nyatla 17:f29e1ca90e3d 8 #include <stdlib.h>
nyatla 17:f29e1ca90e3d 9 #include <typeinfo>
nyatla 17:f29e1ca90e3d 10 #include "mbed.h"
nyatla 17:f29e1ca90e3d 11 #include "FATDirHandle.h"
nyatla 19:33b9ba0859ee 12
nyatla 17:f29e1ca90e3d 13
nyatla 18:1970fec78229 14 using namespace MiMic;
nyatla 111:f525c6e5e27b 15
nyatla 111:f525c6e5e27b 16 static void write_path(HttpdConnection& i_connection,const char* i_buf,const char* i_root_alias,const char* i_f_path)
nyatla 111:f525c6e5e27b 17 {
nyatla 111:f525c6e5e27b 18 if(i_root_alias==NULL){
nyatla 111:f525c6e5e27b 19 i_connection.sendBodyF("%s",i_buf);
nyatla 111:f525c6e5e27b 20 }else{
nyatla 111:f525c6e5e27b 21 i_connection.sendBodyF("%.*s%s",strlen(i_root_alias)-1,i_root_alias,i_buf+strlen(i_f_path)+1);
nyatla 111:f525c6e5e27b 22 }
nyatla 111:f525c6e5e27b 23 }
nyatla 111:f525c6e5e27b 24
nyatla 111:f525c6e5e27b 25 void ModLocalFileSystem::retDirJson(const char* i_buf,HttpdConnection& i_connection,unsigned char i_fs_type)
nyatla 18:1970fec78229 26 {
nyatla 18:1970fec78229 27 //assert(HEAD or GET)
nyatla 18:1970fec78229 28 //directory-list json
nyatla 18:1970fec78229 29 if(!NyLPC_cHttpdUtils_sendJsonHeader((i_connection._ref_inst))){
nyatla 18:1970fec78229 30 return;
nyatla 18:1970fec78229 31 }
nyatla 18:1970fec78229 32 if(!i_connection.isMethodType(Http::MT_GET)){
nyatla 18:1970fec78229 33 return;
nyatla 18:1970fec78229 34 }
nyatla 111:f525c6e5e27b 35
nyatla 111:f525c6e5e27b 36 DIR* d=opendir(i_buf);
nyatla 18:1970fec78229 37 if ( d == NULL )
nyatla 18:1970fec78229 38 {
nyatla 111:f525c6e5e27b 39 i_connection.sendBodyF("{\"dir\":\"");
nyatla 111:f525c6e5e27b 40 write_path(i_connection,i_buf,this->_root_alias,this->_path);
nyatla 111:f525c6e5e27b 41 i_connection.sendBodyF("\",\"status\":404,\"list\":[]}");
nyatla 18:1970fec78229 42 return;
nyatla 18:1970fec78229 43 }
nyatla 18:1970fec78229 44 if(!i_connection.isMethodType(Http::MT_GET)){
nyatla 18:1970fec78229 45 //nothing to do
nyatla 18:1970fec78229 46 }else{
nyatla 111:f525c6e5e27b 47 i_connection.sendBodyF("{\"dir\":\"");
nyatla 111:f525c6e5e27b 48 write_path(i_connection,i_buf,this->_root_alias,this->_path);
nyatla 111:f525c6e5e27b 49 i_connection.sendBodyF("\",\"status\":200,\"list\":[");
nyatla 48:00d211aac2ec 50 switch(i_fs_type){
nyatla 48:00d211aac2ec 51 case ModLocalFileSystem::FST_DEFAULT:
nyatla 48:00d211aac2ec 52 for(struct dirent *p= readdir(d);;)
nyatla 48:00d211aac2ec 53 {
nyatla 48:00d211aac2ec 54 i_connection.sendBodyF("{\"name\":\"%s\",\"mtype\":\"%s\",\"size\":undefined}",
nyatla 48:00d211aac2ec 55 p->d_name,NyLPC_cMiMeType_getFileName2MimeType(p->d_name));
nyatla 48:00d211aac2ec 56 p = readdir(d);
nyatla 48:00d211aac2ec 57 if(p==NULL){
nyatla 48:00d211aac2ec 58 break;
nyatla 48:00d211aac2ec 59 }
nyatla 48:00d211aac2ec 60 i_connection.sendBodyF(",");
nyatla 48:00d211aac2ec 61 }
nyatla 48:00d211aac2ec 62 break;
nyatla 48:00d211aac2ec 63 case ModLocalFileSystem::FST_SDFATFS:
nyatla 48:00d211aac2ec 64 for(struct dirent *p= readdir(d);;)
nyatla 48:00d211aac2ec 65 {
nyatla 18:1970fec78229 66 bool isdir=(((struct direntFAT*)(p))->fattrib & AM_DIR)!=0;
nyatla 18:1970fec78229 67 i_connection.sendBodyF("{\"name\":\"%s\",\"mtype\":\"%s\",\"size\":%u}",
nyatla 18:1970fec78229 68 p->d_name,isdir?"directory":NyLPC_cMiMeType_getFileName2MimeType(p->d_name),
nyatla 18:1970fec78229 69 isdir?0:((struct direntFAT*)(p))->fsize);
nyatla 48:00d211aac2ec 70 p = readdir(d);
nyatla 48:00d211aac2ec 71 if(p==NULL){
nyatla 48:00d211aac2ec 72 break;
nyatla 48:00d211aac2ec 73 }
nyatla 48:00d211aac2ec 74 i_connection.sendBodyF(",");
nyatla 18:1970fec78229 75 }
nyatla 48:00d211aac2ec 76 break;
nyatla 48:00d211aac2ec 77 default:
nyatla 48:00d211aac2ec 78 break;
nyatla 18:1970fec78229 79 }
nyatla 18:1970fec78229 80 i_connection.sendBodyF("]}");
nyatla 18:1970fec78229 81 }
nyatla 18:1970fec78229 82 closedir(d);
nyatla 18:1970fec78229 83 }
nyatla 111:f525c6e5e27b 84 void ModLocalFileSystem::retDirHtml(const char* i_buf,HttpdConnection& i_connection,unsigned char i_fs_type)
nyatla 18:1970fec78229 85 {
nyatla 18:1970fec78229 86 //assert(HEAD or GET)
nyatla 111:f525c6e5e27b 87 DIR* d=opendir(i_buf);
nyatla 18:1970fec78229 88 if(d==NULL){
nyatla 43:a182f2b5ff41 89 i_connection.sendError(403);
nyatla 18:1970fec78229 90 return;
nyatla 18:1970fec78229 91 }
nyatla 18:1970fec78229 92 if(!i_connection.sendHeader(200,"text/html",NULL)){
nyatla 18:1970fec78229 93 //nothing to do
nyatla 18:1970fec78229 94 }else{
nyatla 18:1970fec78229 95 if(!i_connection.isMethodType(Http::MT_GET)){
nyatla 18:1970fec78229 96 //nothing to do.
nyatla 18:1970fec78229 97 }else{
nyatla 18:1970fec78229 98 i_connection.sendBodyF(
nyatla 111:f525c6e5e27b 99 "<!DOCTYPE html><html><body><h1>Index of ");
nyatla 111:f525c6e5e27b 100 write_path(i_connection,i_buf,this->_root_alias,this->_path);
nyatla 111:f525c6e5e27b 101 i_connection.sendBodyF("</h1><hr/>\n"
nyatla 111:f525c6e5e27b 102 "<ul>\n");
nyatla 48:00d211aac2ec 103 switch(i_fs_type){
nyatla 48:00d211aac2ec 104 case ModLocalFileSystem::FST_DEFAULT:
nyatla 48:00d211aac2ec 105 for(struct dirent *p = readdir(d);p!=NULL;p = readdir(d))
nyatla 48:00d211aac2ec 106 {
nyatla 48:00d211aac2ec 107 i_connection.sendBodyF("<li><a href=\"./%s\">%s</a></li>\n",
nyatla 48:00d211aac2ec 108 p->d_name,p->d_name);
nyatla 48:00d211aac2ec 109 }
nyatla 48:00d211aac2ec 110 break;
nyatla 48:00d211aac2ec 111 case ModLocalFileSystem::FST_SDFATFS:
nyatla 48:00d211aac2ec 112 for(struct dirent *p = readdir(d);p!=NULL;p = readdir(d))
nyatla 48:00d211aac2ec 113 {
nyatla 18:1970fec78229 114 if((((struct direntFAT*)(p))->fattrib & AM_DIR)!=0){
nyatla 18:1970fec78229 115 //dir
nyatla 18:1970fec78229 116 i_connection.sendBodyF("<li><a href=\"./%s/\">[DIR]%s</a></li>\n",p->d_name,p->d_name);
nyatla 18:1970fec78229 117 }else{
nyatla 18:1970fec78229 118 //file
nyatla 18:1970fec78229 119 i_connection.sendBodyF("<li><a href=\"./%s\">%s</a></li>\n",p->d_name,p->d_name);
nyatla 18:1970fec78229 120 }
nyatla 18:1970fec78229 121 }
nyatla 48:00d211aac2ec 122 break;
nyatla 48:00d211aac2ec 123 default:
nyatla 48:00d211aac2ec 124 break;
nyatla 18:1970fec78229 125 }
nyatla 111:f525c6e5e27b 126 i_connection.sendBodyF("</ul></body></html>");
nyatla 18:1970fec78229 127 }
nyatla 18:1970fec78229 128 }
nyatla 18:1970fec78229 129 closedir(d);
nyatla 18:1970fec78229 130 }
nyatla 111:f525c6e5e27b 131 /**
nyatla 111:f525c6e5e27b 132 * @param i_path
nyatla 111:f525c6e5e27b 133 * i_pathにはnull終端ファイルパスを入れてください。
nyatla 111:f525c6e5e27b 134 */
nyatla 111:f525c6e5e27b 135 void ModLocalFileSystem::retFile(char* i_buf,HttpdConnection& i_connection)
nyatla 18:1970fec78229 136 {
nyatla 18:1970fec78229 137 //return content
nyatla 111:f525c6e5e27b 138 FILE *fp = fopen(i_buf, "r");
nyatla 18:1970fec78229 139 if(fp==NULL){
nyatla 43:a182f2b5ff41 140 i_connection.sendError(404);
nyatla 18:1970fec78229 141 return;
nyatla 18:1970fec78229 142 }
nyatla 18:1970fec78229 143
nyatla 18:1970fec78229 144 fseek(fp, 0, SEEK_END); // seek to end of file
nyatla 111:f525c6e5e27b 145 size_t sz = ftell(fp); // get current file pointer
nyatla 18:1970fec78229 146 fseek(fp, 0, SEEK_SET); // seek back to beginning of file
nyatla 111:f525c6e5e27b 147 if(i_connection.sendHeader(200,NyLPC_cMiMeType_getFileName2MimeType(i_buf),NULL,sz)){
nyatla 18:1970fec78229 148 if(!i_connection.isMethodType(Http::MT_GET)){
nyatla 18:1970fec78229 149 //nothing to do
nyatla 18:1970fec78229 150 }else{
nyatla 26:1a13899a5a1c 151 Timer t;
nyatla 26:1a13899a5a1c 152 t.start();
nyatla 18:1970fec78229 153 for(;;){
nyatla 111:f525c6e5e27b 154 sz=fread(i_buf,1,Httpd::SIZE_OF_HTTP_BUF,fp);
nyatla 18:1970fec78229 155 if(sz<1){
nyatla 18:1970fec78229 156 break;
nyatla 18:1970fec78229 157 }
nyatla 111:f525c6e5e27b 158 if(!i_connection.sendBody(i_buf,sz)){
nyatla 18:1970fec78229 159 break;
nyatla 18:1970fec78229 160 }
nyatla 43:a182f2b5ff41 161 //switch other session
nyatla 26:1a13899a5a1c 162 if(t.read_ms()>500){
nyatla 26:1a13899a5a1c 163 //switch transport thread
nyatla 21:f2a70f15301f 164 i_connection.unlockHttpd();
nyatla 21:f2a70f15301f 165 NyLPC_cThread_sleep(50);
nyatla 21:f2a70f15301f 166 i_connection.lockHttpd();
nyatla 26:1a13899a5a1c 167 t.reset();
nyatla 21:f2a70f15301f 168 }
nyatla 18:1970fec78229 169 }
nyatla 18:1970fec78229 170 }
nyatla 18:1970fec78229 171 }
nyatla 18:1970fec78229 172 fclose(fp);
nyatla 18:1970fec78229 173 }
nyatla 18:1970fec78229 174
nyatla 111:f525c6e5e27b 175 NyLPC_TBool flip_url_prefix(char* buf, int buf_len, const char* url_prefix, const char* file_path)
nyatla 111:f525c6e5e27b 176 {
nyatla 111:f525c6e5e27b 177 size_t bl = strlen(url_prefix);
nyatla 111:f525c6e5e27b 178 size_t al = strlen(file_path)+2;
nyatla 111:f525c6e5e27b 179 if (al - bl + strlen(buf) + 1 > buf_len){
nyatla 111:f525c6e5e27b 180 return NyLPC_TBool_FALSE;
nyatla 111:f525c6e5e27b 181 }
nyatla 111:f525c6e5e27b 182 if (strncmp(buf, url_prefix, bl) == 0){
nyatla 111:f525c6e5e27b 183 memmove(buf + al, buf + bl, strlen(buf) - bl + 1);
nyatla 111:f525c6e5e27b 184 memcpy(buf + 1, file_path, al - 1);
nyatla 111:f525c6e5e27b 185 *(buf + al - 1) = '/';
nyatla 111:f525c6e5e27b 186 }
nyatla 111:f525c6e5e27b 187 return NyLPC_TBool_TRUE;
nyatla 111:f525c6e5e27b 188 }
nyatla 17:f29e1ca90e3d 189
nyatla 17:f29e1ca90e3d 190 namespace MiMic
nyatla 17:f29e1ca90e3d 191 {
nyatla 111:f525c6e5e27b 192 ModLocalFileSystem::ModLocalFileSystem(const char* i_path,const char* i_root_alias,unsigned char i_fs_type):ModBaseClass()
nyatla 17:f29e1ca90e3d 193 {
nyatla 111:f525c6e5e27b 194 this->setParam(i_path,i_root_alias,i_fs_type);
nyatla 111:f525c6e5e27b 195 }
nyatla 111:f525c6e5e27b 196 ModLocalFileSystem::ModLocalFileSystem(const char* i_path,unsigned char i_fs_type):ModBaseClass()
nyatla 111:f525c6e5e27b 197 {
nyatla 111:f525c6e5e27b 198 this->setParam(i_path,NULL,i_fs_type);
nyatla 17:f29e1ca90e3d 199 }
nyatla 17:f29e1ca90e3d 200 ModLocalFileSystem::ModLocalFileSystem():ModBaseClass()
nyatla 17:f29e1ca90e3d 201 {
nyatla 111:f525c6e5e27b 202 this->_root_alias=NULL;
nyatla 17:f29e1ca90e3d 203 }
nyatla 17:f29e1ca90e3d 204 ModLocalFileSystem::~ModLocalFileSystem()
nyatla 17:f29e1ca90e3d 205 {
nyatla 17:f29e1ca90e3d 206 }
nyatla 111:f525c6e5e27b 207 void ModLocalFileSystem::setParam(const char* i_path,const char* i_root_alias,unsigned char i_fs_type)
nyatla 111:f525c6e5e27b 208 {
nyatla 111:f525c6e5e27b 209 NyLPC_Assert(strlen(i_root_alias)>0);
nyatla 111:f525c6e5e27b 210 ModBaseClass::setParam(i_path);
nyatla 111:f525c6e5e27b 211 this->_root_alias=i_root_alias;
nyatla 111:f525c6e5e27b 212 this->_fs_type=i_fs_type;
nyatla 111:f525c6e5e27b 213 }
nyatla 48:00d211aac2ec 214 void ModLocalFileSystem::setParam(const char* i_path,unsigned char i_fs_type)
nyatla 17:f29e1ca90e3d 215 {
nyatla 111:f525c6e5e27b 216 this->setParam(i_path,NULL,i_fs_type);
nyatla 17:f29e1ca90e3d 217 }
nyatla 111:f525c6e5e27b 218 bool ModLocalFileSystem::canHandle(HttpdConnection& i_connection)
nyatla 111:f525c6e5e27b 219 {
nyatla 111:f525c6e5e27b 220 if(this->_root_alias==NULL){
nyatla 111:f525c6e5e27b 221 return ModBaseClass::canHandle(i_connection);
nyatla 111:f525c6e5e27b 222 }
nyatla 111:f525c6e5e27b 223 //root alias指定がある場合
nyatla 111:f525c6e5e27b 224 if(!NyLPC_cHttpdConnection_getReqStatus(i_connection._ref_inst)==NyLPC_cHttpdConnection_ReqStatus_REQPARSE)
nyatla 111:f525c6e5e27b 225 {
nyatla 111:f525c6e5e27b 226 return NyLPC_TBool_FALSE;
nyatla 111:f525c6e5e27b 227 }
nyatla 111:f525c6e5e27b 228 const NyLPC_TChar* in_url;
nyatla 111:f525c6e5e27b 229 in_url=NyLPC_cHttpdConnection_getUrlPrefix(i_connection._ref_inst);
nyatla 111:f525c6e5e27b 230 size_t base_url_len=strlen(this->_root_alias);
nyatla 111:f525c6e5e27b 231 if(strncmp(this->_root_alias,in_url,base_url_len)!=0){
nyatla 111:f525c6e5e27b 232 return false;
nyatla 111:f525c6e5e27b 233 }
nyatla 111:f525c6e5e27b 234 return true;
nyatla 111:f525c6e5e27b 235 }
nyatla 17:f29e1ca90e3d 236 bool ModLocalFileSystem::execute(HttpdConnection& i_connection)
nyatla 17:f29e1ca90e3d 237 {
nyatla 17:f29e1ca90e3d 238 //check platform
nyatla 17:f29e1ca90e3d 239 //<write here! />
nyatla 17:f29e1ca90e3d 240
nyatla 17:f29e1ca90e3d 241 //check prefix
nyatla 17:f29e1ca90e3d 242 if(!this->canHandle(i_connection)){
nyatla 17:f29e1ca90e3d 243 return false;
nyatla 17:f29e1ca90e3d 244 }
nyatla 43:a182f2b5ff41 245
nyatla 18:1970fec78229 246 //check Method type
nyatla 18:1970fec78229 247 {
nyatla 18:1970fec78229 248 int mt=i_connection.getMethodType();
nyatla 18:1970fec78229 249 if(mt!=Http::MT_GET && mt!=Http::MT_HEAD){
nyatla 18:1970fec78229 250 //method not allowed.
nyatla 43:a182f2b5ff41 251 i_connection.sendError(405);
nyatla 18:1970fec78229 252 return true;
nyatla 18:1970fec78229 253 }
nyatla 18:1970fec78229 254 }
nyatla 17:f29e1ca90e3d 255 //Httpd lock
nyatla 18:1970fec78229 256 i_connection.lockHttpd();
nyatla 19:33b9ba0859ee 257 char* buf=Httpd::_shared_buf;
nyatla 19:33b9ba0859ee 258
nyatla 17:f29e1ca90e3d 259 //set file path
nyatla 17:f29e1ca90e3d 260 {
nyatla 17:f29e1ca90e3d 261 //call ModUrl
nyatla 17:f29e1ca90e3d 262 NyLPC_TcModUrl_t mod;
nyatla 17:f29e1ca90e3d 263 NyLPC_cModUrl_initialize(&mod);
nyatla 19:33b9ba0859ee 264 if(!NyLPC_cModUrl_execute2(&mod,i_connection._ref_inst,buf,Httpd::SIZE_OF_HTTP_BUF,0,NyLPC_cModUrl_ParseMode_ALL)){
nyatla 17:f29e1ca90e3d 265 NyLPC_cModUrl_finalize(&mod);
nyatla 19:33b9ba0859ee 266 i_connection.unlockHttpd();
nyatla 17:f29e1ca90e3d 267 return true;
nyatla 17:f29e1ca90e3d 268 }
nyatla 17:f29e1ca90e3d 269 NyLPC_cModUrl_finalize(&mod);
nyatla 17:f29e1ca90e3d 270 }
nyatla 111:f525c6e5e27b 271 //パスのエイリアスがあるときはここで編集
nyatla 111:f525c6e5e27b 272 if(this->_root_alias!=NULL){
nyatla 111:f525c6e5e27b 273 flip_url_prefix(buf,Httpd::SIZE_OF_HTTP_BUF,this->_root_alias,this->_path);
nyatla 111:f525c6e5e27b 274 }
nyatla 17:f29e1ca90e3d 275 UrlReader url(buf);
nyatla 17:f29e1ca90e3d 276 if(url.hasQueryKey("list")){
nyatla 18:1970fec78229 277 // if path has '/?list' query key,return directory information
nyatla 111:f525c6e5e27b 278 const char* t;
nyatla 111:f525c6e5e27b 279 int l;
nyatla 111:f525c6e5e27b 280 url.getPath(t,l);
nyatla 111:f525c6e5e27b 281 buf[l]='\0';//split path
nyatla 111:f525c6e5e27b 282 //remove '/'
nyatla 111:f525c6e5e27b 283 if(buf[l-1]=='/'){
nyatla 111:f525c6e5e27b 284 buf[l-1]='\0';
nyatla 111:f525c6e5e27b 285 }
nyatla 111:f525c6e5e27b 286 retDirJson(buf,i_connection,this->_fs_type);
nyatla 18:1970fec78229 287 }else if(strchr(buf,'?')==NULL && strchr(buf,'#')==NULL && buf[strlen(buf)-1]=='/'){
nyatla 18:1970fec78229 288 //return directory html when URL has not bookmark and URL query and terminated by '/'.
nyatla 111:f525c6e5e27b 289 buf[strlen(buf)-1]='\0';//convert to dir path
nyatla 111:f525c6e5e27b 290 retDirHtml(buf,i_connection,this->_fs_type);
nyatla 17:f29e1ca90e3d 291 }else{
nyatla 111:f525c6e5e27b 292 //split URL path and query
nyatla 111:f525c6e5e27b 293 const char* t;
nyatla 111:f525c6e5e27b 294 int l;
nyatla 111:f525c6e5e27b 295 url.getPath(t,l);
nyatla 111:f525c6e5e27b 296 buf[l]='\0';
nyatla 111:f525c6e5e27b 297 retFile(buf,i_connection);
nyatla 17:f29e1ca90e3d 298 }
nyatla 17:f29e1ca90e3d 299 //Httpd unlock
nyatla 19:33b9ba0859ee 300 i_connection.unlockHttpd();
nyatla 17:f29e1ca90e3d 301 return true;
nyatla 17:f29e1ca90e3d 302
nyatla 17:f29e1ca90e3d 303 }
nyatla 17:f29e1ca90e3d 304 }