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

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

#1 add filter class

File size: 4.5 KB
Line 
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)
14 : mPort (0)
15{
16 /*----------------------------------------------------------------*/
17 /*----------------------------------------------------------------*/
18}
19
20CRXHttpRequest::~CRXHttpRequest (void)
21{
22 /*----------------------------------------------------------------*/
23 Reset ();
24 /*----------------------------------------------------------------*/
25}
26
27void
28CRXHttpRequest::Reset (void)
29{
30 /*----------------------------------------------------------------*/
31 ResetMessage ();
32 /*----------------------------------------------------------------*/
33}
34
35void
36CRXHttpRequest::SetHeader (const char * aHttpRequest)
37{
38 /*----------------------------------------------------------------*/
39 SetMessage (aHttpRequest);
40 /*----------------------------------------------------------------*/
41}
42
43std::string
44CRXHttpRequest::GetHeader (void) const
45{
46 /*----------------------------------------------------------------*/
47 /*----------------------------------------------------------------*/
48
49 return GetMessage ();
50}
51
52CRXHttpRequest &
53CRXHttpRequest::operator = (const char * aHttpMessage)
54{
55 /*----------------------------------------------------------------*/
56 SetHeader (aHttpMessage);
57 /*----------------------------------------------------------------*/
58
59 return *this;
60}
61
62void
63CRXHttpRequest::Parse (void)
64{
65 std::string aHttpRequestUrl;
66
67 char aMethod[64] = {0x00, };
68 char aUrl[1024 * 4] = {0x00, };
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 /*----------------------------------------------------------------*/
78 aHttpRequestUrl = GetHeader ().substr (0, mHttpMessage.find ('\r'));
79
80 /*----------------------------------------------------------------
81 * 1. separate first line to <METHOD>, <URL> and <VERSION>
82 *----------------------------------------------------------------*/
83 sscanf (aHttpRequestUrl.c_str (), "%[^ ] %[^ ] %[^ ]\r\n", aMethod, aUrl, aHttpVersion);
84 mMethod.assign (aMethod);
85 mUrl.assign (aUrl);
86 mHttpVersion.assign (aHttpVersion);
87
88 ParseFileExtension ();
89
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 /*----------------------------------------------------------------*/
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.