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

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

#1 change logic more detail

File size: 4.2 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
52void
53CRXHttpRequest::Parse (void)
54{
55 std::string aHttpRequestUrl;
56
57 char aMethod[64] = {0x00, };
58 char aUrl[1024 * 4] = {0x00, };
59 char aHttpVersion[64] = {0x00, };
60
61 char aProtocol[32] = {0x00, };
62 char aHostBuffer[512] = {0x00, };
63 char aHost[512] = {0x00, };
64 char aPortBuffer[32] = {0x00, };
65 int aPort = 0;
66
67 /*----------------------------------------------------------------*/
68 aHttpRequestUrl = GetHeader ().substr (0, mHttpMessage.find ('\r'));
69
70 /*----------------------------------------------------------------
71 * 1. separate first line to <METHOD>, <URL> and <VERSION>
72 *----------------------------------------------------------------*/
73 sscanf (aHttpRequestUrl.c_str (), "%[^ ] %[^ ] %[^ ]\r\n", aMethod, aUrl, aHttpVersion);
74 mMethod.assign (aMethod);
75 mUrl.assign (aUrl);
76 mHttpVersion.assign (aHttpVersion);
77
78 ParseFileExtension ();
79
80 /*----------------------------------------------------------------
81 * 2. separate <URL> to <PROTOCOL> and <HOST>
82 *----------------------------------------------------------------*/
83 sscanf (mUrl.c_str (), "%[^://]://%[^/]", aProtocol, aHostBuffer);
84 mProtocol.assign (aProtocol);
85
86 /*----------------------------------------------------------------
87 * 3. separate <HOST> to <HOSTNAME>:<PORT>
88 *----------------------------------------------------------------*/
89 sscanf (aHostBuffer, "%[^:]:%s", aHost, aPortBuffer);
90 mHost.assign (aHost);
91 aPort = atoi (aPortBuffer);
92
93 mPort = aPort == 0 ? 80 : aPort;
94 /*----------------------------------------------------------------*/
95}
96
97void
98CRXHttpRequest::ParseFileExtension (void)
99{
100 std::string aFilename;
101
102 size_t aFilenameOffset = 0;
103 size_t aParameterPosition = 0;
104 size_t aSlashPosition = 0;
105 size_t aDotPosition = 0;
106
107 /*----------------------------------------------------------------*/
108 /*----------------------------------------------------------------
109 * 1. get filename
110 *----------------------------------------------------------------*/
111 aParameterPosition = mUrl.find ('?');
112 aSlashPosition = mUrl.rfind ('/', aParameterPosition) + 1;
113 if (aSlashPosition == mUrl.length ())
114 return ;
115
116 aFilenameOffset = (aParameterPosition != std::string::npos)
117 ? aParameterPosition - aSlashPosition
118 : std::string::npos;
119
120 aFilename = mUrl.substr (aSlashPosition, aFilenameOffset);
121
122 /*----------------------------------------------------------------
123 * 2. get file extension
124 *----------------------------------------------------------------*/
125 aDotPosition = aFilename.rfind ('.');
126 if (aDotPosition != std::string::npos)
127 mFileExtension = aFilename.substr (aDotPosition + 1);
128 /*----------------------------------------------------------------*/
129}
130
131std::string CRXHttpRequest::GetURL (void) const { return mUrl; }
132std::string CRXHttpRequest::GetHost (void) const { return mHost; }
133int CRXHttpRequest::GetPort (void) const { return mPort; }
134std::string CRXHttpRequest::GetFileExtension (void) const { return mFileExtension; }
Note: See TracBrowser for help on using the repository browser.