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

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NyLPC_cHttpBodyWriter.c Source File

NyLPC_cHttpBodyWriter.c

00001 /*********************************************************************************
00002  * PROJECT: MiMic
00003  * --------------------------------------------------------------------------------
00004  *
00005  * This file is part of MiMic
00006  * Copyright (C)2011 Ryo Iizuka
00007  *
00008  * MiMic is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU Lesser General Public License as published
00010  * by the Free Software Foundation, either version 3 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public License
00019  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020  *
00021  * For further information please contact.
00022  *  http://nyatla.jp/
00023  *  <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>
00024  *
00025  *********************************************************************************/
00026 
00027 
00028 #include <stdlib.h>
00029 #include "NyLPC_utils.h"
00030 #include "NyLPC_cHttpBodyWriter.h"
00031 
00032 
00033 
00034 
00035 
00036 
00037 /**
00038  * PrintHandler
00039  */
00040 static NyLPC_TBool printHandler(void* i_inst,const void* i_buf,NyLPC_TUInt32 i_len)
00041 {
00042     //エラー状態ならFALSE
00043     if(((NyLPC_TcHttpBodyWriter_t*)i_inst)->_is_error){
00044         return NyLPC_TBool_FALSE;
00045     }
00046     ((NyLPC_TcHttpBodyWriter_t*)i_inst)->_size_of_sent+=i_len;
00047     if(!NyLPC_iHttpPtrStream_write(((NyLPC_TcHttpBodyWriter_t*)i_inst)->_ref_stream,i_buf,i_len)){
00048         ((NyLPC_TcHttpBodyWriter_t*)i_inst)->_is_error=NyLPC_TUInt8_TRUE;
00049         return NyLPC_TBool_FALSE;
00050     }
00051     return NyLPC_TBool_TRUE;
00052 }
00053 
00054 
00055 
00056 void NyLPC_cHttpBodyWriter_initialize(NyLPC_TcHttpBodyWriter_t* i_inst,NyLPC_TcHttpStream_t* i_stream)
00057 {
00058     i_inst->_ref_stream=&(i_stream->super);
00059     i_inst->_is_chunked=NyLPC_TUInt8_FALSE;
00060     i_inst->_is_error=NyLPC_TUInt8_FALSE;
00061     i_inst->_size_of_sent=0;
00062     i_inst->_content_length=0;
00063     NyLPC_iHttpPtrStream_setWriteEncoding(i_inst->_ref_stream,NyLPC_TiHttpPtrStream_ET_NONE);
00064 }
00065 
00066 void NyLPC_cHttpBodyWriter_setChunked(NyLPC_TcHttpBodyWriter_t* i_inst)
00067 {
00068     i_inst->_is_chunked=NyLPC_TUInt8_TRUE;
00069     NyLPC_iHttpPtrStream_setWriteEncoding(i_inst->_ref_stream,NyLPC_TiHttpPtrStream_ET_CHUNKED);
00070 }
00071 void NyLPC_cHttpBodyWriter_setContentLength(NyLPC_TcHttpBodyWriter_t* i_inst,NyLPC_TUInt32 i_content_length)
00072 {
00073     i_inst->_is_chunked=NyLPC_TUInt8_FALSE;
00074     i_inst->_content_length=i_content_length;
00075     NyLPC_iHttpPtrStream_setWriteEncoding(i_inst->_ref_stream,NyLPC_TiHttpPtrStream_ET_NONE);
00076 }
00077 
00078 
00079 /**
00080  * HttpBodyを書き込みます。
00081  * @return
00082  * 偽を返した場合は、コネクションを切断してください。
00083  */
00084 NyLPC_TBool NyLPC_cHttpBodyWriter_write(NyLPC_TcHttpBodyWriter_t* i_inst,const void* i_buf,NyLPC_TUInt32 i_len)
00085 {
00086     return printHandler(i_inst,i_buf,i_len);
00087 }
00088 
00089 /**
00090  * HttpBodyの書き込みを完了します。
00091  * @return
00092  */
00093 NyLPC_TBool NyLPC_cHttpBodyWriter_close(NyLPC_TcHttpBodyWriter_t* i_inst)
00094 {
00095     //エラー状態ならFALSE
00096     if(i_inst->_is_error){
00097         return NyLPC_TBool_FALSE;
00098     }
00099     //chunkedの場合、フッタを書き込む
00100     if(i_inst->_is_chunked){
00101         //エンコーディングを戻す。
00102         NyLPC_iHttpPtrStream_setWriteEncoding(i_inst->_ref_stream,NyLPC_TiHttpPtrStream_ET_NONE);
00103         //フッタを書き込む。
00104         if(!NyLPC_iHttpPtrStream_write(i_inst->_ref_stream,"0\r\n\r\n",5)){
00105             i_inst->_is_error=NyLPC_TUInt8_TRUE;
00106             return NyLPC_TBool_FALSE;
00107         }
00108     }
00109     //エラーでないときはストリームをフラッシュ
00110     NyLPC_iHttpPtrStream_flush(i_inst->_ref_stream);
00111     //クローズのステータスで状態を変える。
00112     return NyLPC_TBool_TRUE;
00113 }
00114 
00115 /**
00116  * printfライクな書式出力を提供します。
00117  * @i_fmt
00118  * 書式文字列です。%d,%x,%s,%c,%%をサポートします。
00119  */
00120 NyLPC_TBool NyLPC_cHttpBodyWriter_format(NyLPC_TcHttpBodyWriter_t* i_inst,const NyLPC_TChar* i_fmt,...)
00121 {
00122     NyLPC_TBool ret;
00123     va_list a;
00124     //エラー状態ならFALSE
00125     if(i_inst->_is_error){
00126         return NyLPC_TBool_FALSE;
00127     }
00128     va_start(a,i_fmt);
00129     ret=   NyLPC_cFormatWriter_print(printHandler,i_inst,i_fmt,a);
00130     va_end(a);
00131     return ret;
00132 }
00133 
00134 NyLPC_TBool NyLPC_cHttpBodyWriter_formatV(NyLPC_TcHttpBodyWriter_t* i_inst,const NyLPC_TChar* i_fmt,va_list i_args)
00135 {
00136     NyLPC_TBool ret;
00137     //エラー状態ならFALSE
00138     if(i_inst->_is_error){
00139         return NyLPC_TBool_FALSE;
00140     }
00141     ret=NyLPC_cFormatWriter_print(printHandler,i_inst,i_fmt,i_args);
00142     return ret;
00143 }
00144 
00145 /**
00146  * テスト用のコード。
00147  */
00148 #define TEST
00149 #ifndef TEST
00150 //テスト
00151 #include "NyLPC_cHttpHeaderWriter.h"
00152 
00153 const char* TP1=
00154         "HTTP/0.9 200 OK\r\n"
00155         "HOST: 127.0.0.0.0.0.1\r\n"
00156         "CONTENt-LENGTH: 1285\r\n"
00157         "CONNECTION: CloSe\r\n"
00158         "ETAG: nyatla.jp\r\n"
00159         "ETAG: nyatla.jp\r\n"
00160         "Transfer-Encoding:chunked\r\n"
00161         "\r\n";
00162 const char* TP2=
00163         "GET /nyanyanya!/nyoronnnnnnnnnnnn?m,fpeofjregnoegnr HTTP/1.1\r\n"
00164         "HOST: 127.0.0.0.0.0.1\r\n"
00165         "CONTENt-LENGTH: 1285\r\n"
00166         "CONNECTION: Keep\r\n"
00167         "ETAG: nyatla.jp\r\n"
00168         "ETAG: nyatla.jp\r\n"
00169         "\r\n";
00170 const char* DT="0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
00171 
00172 
00173 
00174 
00175 
00176 
00177 void main()
00178 {
00179     NyLPC_TcHttpStream_t st;
00180     NyLPC_TcHttpBasicHeaderParser_t hp;
00181     struct NyLPC_THttpBasicHeader reqheader;
00182     NyLPC_TcHttpHeaderWriter_t hw;
00183     NyLPC_TcHttpBodyWriter_t bw;
00184     NyLPC_TcTcpSocket_t ts;
00185     int body_len;
00186     NyLPC_cTcpSocket_initialized(NULL,TP2,strlen(TP2));
00187 
00188     //TCPのオープン
00189     if(!NyLPC_cHttpStream_initialize(&st,&ts)){
00190         //エラー
00191     }
00192     for(;;){
00193         //ヘッダ解析
00194         NyLPC_cHttpBasicHeaderParser_initialize(&hp);
00195         if(!NyLPC_cHttpShortRequestHeaderParser_parse(&hp,&st,&reqheader)){
00196             //エラー
00197             puts("Error");
00198         }
00199         //ヘッダの内容確認
00200         if(reqheader.type!=NyLPC_THttpHeaderType_REQUEST){
00201             //BadRequest
00202             puts("Error");
00203         }
00204         if(reqheader.startline.req.method!=NyLPC_THttpMethodType_GET){
00205             //リクエストサポートしてない
00206             puts("Error");
00207         }
00208         //
00209         NyLPC_cHttpHeaderWriter_initialize(&hw,&st,&reqheader);
00210 //      NyLPC_cHttpResponseWriter_setClose(&hw);
00211         body_len=100;
00212         NyLPC_cHttpHeaderWriter_setContentLength(&hw,body_len);
00213         NyLPC_cHttpHeaderWriter_writeResponseHeader(&hw,500);
00214         NyLPC_cHttpHeaderWriter_close(&hw);
00215 
00216         NyLPC_cHttpBodyWriter_initialize(&bw,&st);
00217         NyLPC_cHttpBodyWriter_setChunked(&bw);
00218         NyLPC_cHttpBodyWriter_write(&bw,"TEST",4);
00219         NyLPC_cHttpBodyWriter_printf(&bw,"TEST");
00220         NyLPC_cHttpBodyWriter_printf(&bw,"TEST[%s][%d][%c],%%,[%x]","abcde",123,'s',0xff0011);
00221         NyLPC_cHttpBodyWriter_close(&bw);
00222         NyLPC_cHttpHttpWriter_finalize(&hw);
00223     }
00224     NyLPC_cHttpStream_finalize(&st);
00225     //TCPのクローズ
00226     return;
00227 }
00228 #endif