diff --git a/demo/restart.sh b/demo/restart.sh index 0bebf67..0ef7154 100755 --- a/demo/restart.sh +++ b/demo/restart.sh @@ -1,4 +1,7 @@ #!/bin/sh -sh start.sh +echo "stop ..." sh stop.sh +echo "start ..." +sh start.sh + diff --git a/demo/server.cpp b/demo/server.cpp index 7dc0285..8af0c37 100644 --- a/demo/server.cpp +++ b/demo/server.cpp @@ -26,8 +26,12 @@ class ServerDemo: public IRequestHandler public: virtual bool do_GET(const HttpReqInfo& httpReq, string& strSnd) { - //HttpReqInfo info = httpReq; - strSnd = httpReq.toString(); + string sentence, tmp; + vector words; + httpReq.GET("key", tmp); + URLDecode(tmp, sentence); + _segment.cut(sentence, words); + vecToString(words, strSnd); return true; } private: diff --git a/husky/HttpReqInfo.hpp b/husky/HttpReqInfo.hpp index 97e537a..68bae8d 100644 --- a/husky/HttpReqInfo.hpp +++ b/husky/HttpReqInfo.hpp @@ -16,6 +16,65 @@ namespace Husky static const char* const KEY_PATH = "PATH"; static const char* const KEY_PROTOCOL = "PROTOCOL"; + typedef unsigned char BYTE; + + inline BYTE toHex(BYTE x) + { + return x > 9 ? x -10 + 'A': x + '0'; + } + + inline BYTE fromHex(BYTE x) + { + return isdigit(x) ? x-'0' : x-'A'+10; + } + + inline void URLEncode(const string &sIn, string& sOut) + { + for( size_t ix = 0; ix < sIn.size(); ix++ ) + { + BYTE buf[4]; + memset( buf, 0, 4 ); + if( isalnum( (BYTE)sIn[ix] ) ) + { + buf[0] = sIn[ix]; + } + //else if ( isspace( (BYTE)sIn[ix] ) ) //貌似把空格编码成%20或者+都可以 + //{ + // buf[0] = '+'; + //} + else + { + buf[0] = '%'; + buf[1] = toHex( (BYTE)sIn[ix] >> 4 ); + buf[2] = toHex( (BYTE)sIn[ix] % 16); + } + sOut += (char *)buf; + } + }; + + inline void URLDecode(const string &sIn, string& sOut) + { + for( size_t ix = 0; ix < sIn.size(); ix++ ) + { + BYTE ch = 0; + if(sIn[ix]=='%') + { + ch = (fromHex(sIn[ix+1])<<4); + ch |= fromHex(sIn[ix+2]); + ix += 2; + } + else if(sIn[ix] == '+') + { + ch = ' '; + } + else + { + ch = sIn[ix]; + } + sOut += (char)ch; + } + } + class HttpReqInfo { public: @@ -45,8 +104,8 @@ namespace Husky { _parseUrl(firstline, _methodGetMap); } - - + + lpos = rpos + 1; if(lpos >= headerStr.size()) { @@ -77,7 +136,7 @@ namespace Husky //message header end //body begin - + return true; } public: