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
sh start.sh
echo "stop ..."
sh stop.sh
echo "start ..."
sh start.sh

View File

@ -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<string> words;
httpReq.GET("key", tmp);
URLDecode(tmp, sentence);
_segment.cut(sentence, words);
vecToString(words, strSnd);
return true;
}
private:

View File

@ -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: