33 //*******************************************************************************************************
34 //MemBufferCreate:
35 // Passed a MemBuffer structure, will allocate a memory buffer
36 // of MEM_BUFFER_SIZE. This buffer can then grow as needed.
37 //*******************************************************************************************************
38 void Request::MemBufferCreate(MemBuffer *b)
39 {
40 b->size = MEM_BUFFER_SIZE;
41 b->buffer =(unsigned char *) malloc( b->size );
42 b->position = b->buffer;
43 }
44
45 //*******************************************************************************************************
46 // MemBufferGrow:
47 // Double the size of the buffer that was passed to this function.
48 //*******************************************************************************************************
49 void Request::MemBufferGrow(MemBuffer *b)
50 {
51 size_t sz;
52 sz = b->position - b->buffer;
53 b->size = b->size *2;
54 b->buffer =(unsigned char *) realloc(b->buffer,b->size);
55 b->position = b->buffer + sz; // readjust current position
56 }
57
58 //*******************************************************************************************************
59 // MemBufferAddByte:
60 // Add a single byte to the memory buffer, grow if needed.
61 //*******************************************************************************************************
62 void Request::MemBufferAddByte(MemBuffer *b,unsigned char byt)
63 {
64 if( (size_t)(b->position-b->buffer) >= b->size )
65 MemBufferGrow(b);
66
67 *(b->position++) = byt;
68 }
69
70 //*******************************************************************************************************
71 // MemBufferAddBuffer:
72 // Add a range of bytes to the memory buffer, grow if needed.
73 //*******************************************************************************************************
74 void Request::MemBufferAddBuffer(MemBuffer *b,
75 unsigned char *buffer, size_t size)
76 {
77 while( ((size_t)(b->position-b->buffer)+size) >= b->size )
78 MemBufferGrow(b);
79
80 memcpy(b->position,buffer,size);
81 b->position+=size;
82 }
83
84 //*******************************************************************************************************
85 // GetHostAddress:
86 // Resolve using DNS or similar(WINS,etc) the IP
87 // address for a domain name such as www.wdj.com.
88 //*******************************************************************************************************
89 DWORD Request::GetHostAddress(LPCSTR host)
90 {
91 struct hostent *phe;
92 char *p;
93
94 phe = gethostbyname( host );
95
96 if(phe==NULL)
97 return 0;
98
99 p = *phe->h_addr_list;
100 return *((DWORD*)p);
101 }
102
103 //*******************************************************************************************************
104 // SendString:
105 // Send a string(null terminated) over the specified socket.
106 //*******************************************************************************************************
107 void Request::SendString(SOCKET sock,LPCSTR str)
108 {
109 send(sock,str,strlen(str),0);
110 }
111
112 //*******************************************************************************************************
113 // ValidHostChar:
114 // Return TRUE if the specified character is valid
115 // for a host name, i.e. A-Z or 0-9 or -.:
116 //*******************************************************************************************************
117 BOOL Request::ValidHostChar(char ch)
118 {
119 return( isalpha(ch) || isdigit(ch)
120 || ch=='-' || ch=='.' || ch==':' );
121 }
122
123
124 //*******************************************************************************************************
125 // ParseURL:
126 // Used to break apart a URL such as
127 // http://www.localhost.com:80/TestPost.htm into protocol, port, host and request.
128 //*******************************************************************************************************
129 void Request::ParseURL(string url,LPSTR protocol,int lprotocol,LPSTR host,int lhost,LPSTR request,int lrequest,int *port)
130 {
131 char *work,*ptr,*ptr2;
132
133 *protocol = *host = *request = 0;
134 *port=80;
135
136 work = strdup(url.c_str());
137 strupr(work);
138
139 ptr = strchr(work,':'); // find protocol if any
140 if(ptr!=NULL)
141 {
142 *(ptr++) = 0;
143 lstrcpyn(protocol,work,lprotocol);
144 }
145 else
146 {
147 lstrcpyn(protocol,"HTTP",lprotocol);
148 ptr = work;
149 }
150
151 if( (*ptr=='/') && (*(ptr+1)=='/') ) // skip past opening /'s
152 ptr+=2;
153
154 ptr2 = ptr; // find host
155 while( ValidHostChar(*ptr2) && *ptr2 )
156 ptr2++;
157
158 *ptr2=0;
159 lstrcpyn(host,ptr,lhost);
160
161 lstrcpyn(request,url.c_str() + (ptr2-work),lrequest); // find the request
162
163 ptr = strchr(host,':'); // find the port number, if any
164 if(ptr!=NULL)
165 {
166 *ptr=0;
167 *port = atoi(ptr+1);
168 }
169