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

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

#1 rename http message to http header and change way to process response content as binary

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 /*----------------------------------------------------------------*/
24}
25
26void
27CRXHttpRequest::SetHeader (const char * aHeader)
28{
29 /*----------------------------------------------------------------*/
30 Set (aHeader);
31 /*----------------------------------------------------------------*/
32}
33
34std::string
35CRXHttpRequest::GetHeader (void) const
36{
37 /*----------------------------------------------------------------*/
38 /*----------------------------------------------------------------*/
39
40 return Get ();
41}
42
43void
44CRXHttpRequest::Parse (void)
45{
46 std::string aHttpRequestUrl;
47
48 char aMethod[64] = {0x00, };
49 char aUrl[1024 * 4] = {0x00, };
50 char aHttpVersion[64] = {0x00, };
51
52 char aProtocol[32] = {0x00, };
53 char aHostBuffer[512] = {0x00, };
54 char aHost[512] = {0x00, };
55 char aPortBuffer[32] = {0x00, };
56 int aPort = 0;
57
58 /*----------------------------------------------------------------*/
59 aHttpRequestUrl = GetHeader ().substr (0, mHttpHeader.find ('\r'));
60
61 /*----------------------------------------------------------------
62 * 1. separate first line to <METHOD>, <URL> and <VERSION>
63 *----------------------------------------------------------------*/
64 sscanf (aHttpRequestUrl.c_str (), "%[^ ] %[^ ] %[^ ]\r\n", aMethod, aUrl, aHttpVersion);
65 mMethod.assign (aMethod);
66 mUrl.assign (aUrl);
67 mHttpVersion.assign (aHttpVersion);
68
69 ParseFileExtension ();
70
71 /*----------------------------------------------------------------
72 * 2. separate <URL> to <PROTOCOL> and <HOST>
73 *----------------------------------------------------------------*/
74 sscanf (mUrl.c_str (), "%[^://]://%[^/]", aProtocol, aHostBuffer);
75 mProtocol.assign (aProtocol);
76
77 /*----------------------------------------------------------------
78 * 3. separate <HOST> to <HOSTNAME>:<PORT>
79 *----------------------------------------------------------------*/
80 sscanf (aHostBuffer, "%[^:]:%s", aHost, aPortBuffer);
81 mHost.assign (aHost);
82 aPort = atoi (aPortBuffer);
83
84 mPort = aPort == 0 ? 80 : aPort;
85 /*----------------------------------------------------------------*/
86}
87
88void
89CRXHttpRequest::ParseFileExtension (void)
90{
91 std::string aFilename;
92
93 size_t aFilenameOffset = 0;
94 size_t aParameterPosition = 0;
95 size_t aSlashPosition = 0;
96 size_t aDotPosition = 0;
97
98 /*----------------------------------------------------------------*/
99 /*----------------------------------------------------------------
100 * 1. get filename
101 *----------------------------------------------------------------*/
102 aParameterPosition = mUrl.find ('?');
103 aSlashPosition = mUrl.rfind ('/', aParameterPosition) + 1;
104 if (aSlashPosition == mUrl.length ())
105 return ;
106
107 aFilenameOffset = (aParameterPosition != std::string::npos)
108 ? aParameterPosition - aSlashPosition
109 : std::string::npos;
110
111 aFilename = mUrl.substr (aSlashPosition, aFilenameOffset);
112
113 /*----------------------------------------------------------------
114 * 2. get file extension
115 *----------------------------------------------------------------*/
116 aDotPosition = aFilename.rfind ('.');
117 if (aDotPosition != std::string::npos)
118 mFileExtension = aFilename.substr (aDotPosition + 1);
119 /*----------------------------------------------------------------*/
120}
121
122std::string
123CRXHttpRequest::GetURL (void) const
124{
125 /*----------------------------------------------------------------*/
126 /*----------------------------------------------------------------*/
127
128 return mUrl;
129}
130
131std::string
132CRXHttpRequest::GetHost (void) const
133{
134 /*----------------------------------------------------------------*/
135 /*----------------------------------------------------------------*/
136
137 return mHost;
138}
139
140int
141CRXHttpRequest::GetPort (void) const
142{
143 /*----------------------------------------------------------------*/
144 /*----------------------------------------------------------------*/
145
146 return mPort;
147}
148
149std::string
150CRXHttpRequest::GetFileExtension (void) const
151{
152 /*----------------------------------------------------------------*/
153 /*----------------------------------------------------------------*/
154
155 return mFileExtension;
156}
Note: See TracBrowser for help on using the repository browser.