source: cheroxy/trunk/src/CRXHttpRequest.cpp@ 28

Last change on this file since 28 was 28, checked in by cheese, 12 years ago

#1 add filter class

File size: 4.5 KB
RevLine 
[6]1/**
2 * CRXHttpRequest.cpp
3 */
4#ifdef _WIN32
5# pragma warning (disable:4996)
6#endif
7
8#include "CRXHttpRequest.h"
9
10#include <stdio.h>
11#include <stdlib.h>
12
13CRXHttpRequest::CRXHttpRequest (void)
[18]14 : mPort (0)
[6]15{
[26]16 /*----------------------------------------------------------------*/
17 /*----------------------------------------------------------------*/
[6]18}
19
[26]20CRXHttpRequest::~CRXHttpRequest (void)
21{
22 /*----------------------------------------------------------------*/
23 Reset ();
24 /*----------------------------------------------------------------*/
25}
26
[6]27void
[26]28CRXHttpRequest::Reset (void)
29{
30 /*----------------------------------------------------------------*/
31 ResetMessage ();
32 /*----------------------------------------------------------------*/
33}
34
35void
[24]36CRXHttpRequest::SetHeader (const char * aHttpRequest)
[6]37{
[26]38 /*----------------------------------------------------------------*/
[6]39 SetMessage (aHttpRequest);
[26]40 /*----------------------------------------------------------------*/
[6]41}
42
43std::string
[24]44CRXHttpRequest::GetHeader (void) const
[6]45{
[26]46 /*----------------------------------------------------------------*/
47 /*----------------------------------------------------------------*/
48
[6]49 return GetMessage ();
50}
51
52CRXHttpRequest &
53CRXHttpRequest::operator = (const char * aHttpMessage)
54{
[26]55 /*----------------------------------------------------------------*/
[24]56 SetHeader (aHttpMessage);
[26]57 /*----------------------------------------------------------------*/
58
[6]59 return *this;
60}
61
62void
63CRXHttpRequest::Parse (void)
64{
[28]65 std::string aHttpRequestUrl;
[6]66
67 char aMethod[64] = {0x00, };
[17]68 char aUrl[1024 * 4] = {0x00, };
[6]69 char aHttpVersion[64] = {0x00, };
70
71 char aProtocol[32] = {0x00, };
72 char aHostBuffer[512] = {0x00, };
73 char aHost[512] = {0x00, };
74 char aPortBuffer[32] = {0x00, };
75 int aPort = 0;
76
77 /*----------------------------------------------------------------*/
[28]78 aHttpRequestUrl = GetHeader ().substr (0, mHttpMessage.find ('\r'));
[6]79
80 /*----------------------------------------------------------------
81 * 1. separate first line to <METHOD>, <URL> and <VERSION>
82 *----------------------------------------------------------------*/
[28]83 sscanf (aHttpRequestUrl.c_str (), "%[^ ] %[^ ] %[^ ]\r\n", aMethod, aUrl, aHttpVersion);
[6]84 mMethod.assign (aMethod);
85 mUrl.assign (aUrl);
86 mHttpVersion.assign (aHttpVersion);
87
[28]88 ParseFileExtension ();
89
[6]90 /*----------------------------------------------------------------
91 * 2. separate <URL> to <PROTOCOL> and <HOST>
92 *----------------------------------------------------------------*/
93 sscanf (mUrl.c_str (), "%[^://]://%[^/]", aProtocol, aHostBuffer);
94 mProtocol.assign (aProtocol);
95
96 /*----------------------------------------------------------------
97 * 3. separate <HOST> to <HOSTNAME>:<PORT>
98 *----------------------------------------------------------------*/
99 sscanf (aHostBuffer, "%[^:]:%s", aHost, aPortBuffer);
100 mHost.assign (aHost);
101 aPort = atoi (aPortBuffer);
102
103 mPort = aPort == 0 ? 80 : aPort;
104 /*----------------------------------------------------------------*/
[28]105}
106
107void
108CRXHttpRequest::ParseFileExtension (void)
109{
110 std::string aFilename;
111
112 size_t aFilenameOffset = 0;
113 size_t aParameterPosition = 0;
114 size_t aSlashPosition = 0;
115 size_t aDotPosition = 0;
116
117 /*----------------------------------------------------------------*/
118 /*----------------------------------------------------------------
119 * 1. get filename
120 *----------------------------------------------------------------*/
121 aParameterPosition = mUrl.find ('?');
122 aSlashPosition = mUrl.rfind ('/', aParameterPosition) + 1;
123 if (aSlashPosition == mUrl.length ())
124 return ;
125
126 aFilenameOffset = (aParameterPosition != std::string::npos)
127 ? aParameterPosition - aSlashPosition
128 : std::string::npos;
129
130 aFilename = mUrl.substr (aSlashPosition, aFilenameOffset);
131
132 /*----------------------------------------------------------------
133 * 2. get file extension
134 *----------------------------------------------------------------*/
135 aDotPosition = aFilename.rfind ('.');
136 if (aDotPosition != std::string::npos)
137 mFileExtension = aFilename.substr (aDotPosition + 1);
138 /*----------------------------------------------------------------*/
139}
140
141std::string CRXHttpRequest::GetURL (void) const { return mUrl; }
142std::string CRXHttpRequest::GetHost (void) const { return mHost; }
143int CRXHttpRequest::GetPort (void) const { return mPort; }
144std::string CRXHttpRequest::GetFileExtension (void) const { return mFileExtension; }
Note: See TracBrowser for help on using the repository browser.