add husky server.cpp into demo

This commit is contained in:
wyy 2013-10-27 06:51:44 -07:00
parent b2d6fbbbe5
commit 9cf476086f
3 changed files with 72 additions and 6 deletions

View File

@ -1,4 +1,7 @@
#!/bin/sh #!/bin/sh
sh start.sh echo "stop ..."
sh stop.sh sh stop.sh
echo "start ..."
sh start.sh

View File

@ -26,8 +26,12 @@ class ServerDemo: public IRequestHandler
public: public:
virtual bool do_GET(const HttpReqInfo& httpReq, string& strSnd) virtual bool do_GET(const HttpReqInfo& httpReq, string& strSnd)
{ {
//HttpReqInfo info = httpReq; string sentence, tmp;
strSnd = httpReq.toString(); vector<string> words;
httpReq.GET("key", tmp);
URLDecode(tmp, sentence);
_segment.cut(sentence, words);
vecToString(words, strSnd);
return true; return true;
} }
private: private:

View File

@ -16,6 +16,65 @@ namespace Husky
static const char* const KEY_PATH = "PATH"; static const char* const KEY_PATH = "PATH";
static const char* const KEY_PROTOCOL = "PROTOCOL"; 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 class HttpReqInfo
{ {
public: public:
@ -45,8 +104,8 @@ namespace Husky
{ {
_parseUrl(firstline, _methodGetMap); _parseUrl(firstline, _methodGetMap);
} }
lpos = rpos + 1; lpos = rpos + 1;
if(lpos >= headerStr.size()) if(lpos >= headerStr.size())
{ {
@ -77,7 +136,7 @@ namespace Husky
//message header end //message header end
//body begin //body begin
return true; return true;
} }
public: public: