File: comhttpsocket.cpp

package info (click to toggle)
musicbrainz 1.0.1.final-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,168 kB
  • ctags: 2,610
  • sloc: ansic: 16,052; sh: 8,658; cpp: 6,283; perl: 327; makefile: 112
file content (277 lines) | stat: -rw-r--r-- 7,316 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/***************************************************************************
                          MBCOMHTTPSocket.cpp  -  description
                             -------------------
    begin                : Sat Sep 29 2000
    copyright            : (C) 2000 by Relatable 
    written by           : Sean Ward
    email                : sward@relatable.com
 ***************************************************************************/
#ifdef WIN32
#pragma warning(disable:4786)
#endif

#include "comhttpsocket.h"
#ifdef WIN32
#include "wincomsocket.h"
#else
#include "comsocket.h"
#endif
#include <stdlib.h>
#include <assert.h>

const char* g_strCOMVer = "Freeamp_2.1.1.0";

MBCOMHTTPSocket::MBCOMHTTPSocket()
{
	m_pSock = new MBCOMSocket;
	m_pTempBuf = NULL;
	m_nBufLen = 0;
	memset(m_hostname, 0x00, 65);
	memset(m_proxyname, 0x00, 1025);
	m_pFile = NULL;
}
MBCOMHTTPSocket::~MBCOMHTTPSocket()
{
	if (m_pSock->IsConnected()) m_pSock->Disconnect();
	delete m_pSock;
	if (m_pTempBuf != NULL) delete [] m_pTempBuf;
}

/** Connects a socket to pIP, on nPort, of type nType. */
int MBCOMHTTPSocket::Connect(const char* pURL)
{
	assert(pURL != NULL);
	if (this->IsConnected()) this->Disconnect();
	if (m_pTempBuf != NULL)
	{
		delete [] m_pTempBuf;
		m_pTempBuf = NULL;
		m_nBufLen = 0;
	}
	// if its not an http:// based url, this can't deal with it
//	if (strncasecmp(pURL, "http://", 7)) return -1;
	if (strncmp(pURL, "http://", 7)) return -1;
	m_strURL = pURL;

	int nRes = 0;
	memset(m_hostname, 0x00, 65);
    	memset(m_proxyname, 0x00, 1025);
    	m_pFile = NULL;
	unsigned short nPort = 80;
	int nNumFields = 0;
	
	if (!m_strProxyAddr.empty())
	{
		nNumFields = sscanf(m_strProxyAddr.c_str(), 
				"http://%[^:/]:%hu", m_hostname, &nPort);
		strcpy(m_proxyname, pURL);
		
		m_pFile = m_proxyname;
	}
	else
	{
		nNumFields = sscanf(m_strURL.c_str(), 
				"http://%[^:/]:%hu", m_hostname, &nPort);
		
		m_pFile = strchr(m_strURL.c_str() + 7, '/');
	}
	
	if (nNumFields < 1)
	{
		return -1; // screwed url
	}
	if (nNumFields < 2)
	{
		nPort = 80;
	}
	
	nRes = m_pSock->Connect((const char*)m_hostname, nPort, SOCK_STREAM);
	return nRes;	
}

/** Disconnects the current socket */
int MBCOMHTTPSocket::Disconnect()
{
	return m_pSock->Disconnect();
}

/** Checks if there is a current open connection */
bool MBCOMHTTPSocket::IsConnected()
{
	return m_pSock->IsConnected();
}

/** Writes to a socket, from buffer pBuffer, up to nLen bytes, and returns the number of written bytes in pnBytesWritten. */
int MBCOMHTTPSocket::Write(const char* pBuffer, int nLen, int* pnBytesWritten)
{
	if (!m_pSock->IsConnected()) return -1; // no connection
	const char* pRequest = "POST %s HTTP/1.0\r\n"
                                "Host: %s\r\n"
                                "Accept: */*\r\n"
                                "User-Agent: %s\r\n"
                                "Content-type: application/octet-stream\r\n"
                                "Content-length: %d\r\n";
	
	int nReqLen = strlen(pRequest) + strlen(m_pFile)
			+ strlen(m_hostname)
			+ strlen(g_strCOMVer)
			+ nLen + 2;
	char* pReq = new char[nReqLen];
	assert(pReq != NULL);
	
	sprintf(pReq, pRequest, m_pFile, m_hostname, g_strCOMVer, nLen);
	
	strcat(pReq, "\r\n");
	
	memcpy(pReq + strlen(pReq), pBuffer, nLen);
	int nBytes = 0;
	int nRes = m_pSock->Write(pReq, nReqLen, &nBytes);
	delete [] pReq;
	
	if ((nRes == 0) && (nBytes == nReqLen))
	{
		*pnBytesWritten = nLen;
	}
	else
	{
		*pnBytesWritten = 0; // something weird happened
	}
	return nRes;
}

/** Reads in a non blocking fashion (ie, selects and polls) for nTimeout seconds */
int MBCOMHTTPSocket::NBRead(char* pBuffer, int nLen, int* nBytesWritten, int nTimeout)
{
	if (!m_pSock->IsConnected()) return -1; // no connection
	
	char HeaderBuffer[1024];	// read up to 1024 bytes for the header
	memset(HeaderBuffer, 0x00, 1024);
	int nBytes = 0;
	int nTotal = 0;
	
	int nRes = m_pSock->NBRead(HeaderBuffer, 1023, &nBytes, nTimeout);
	if (nRes != 0) return -1;	// error reading from socket. server crash?
	
	nTotal = nBytes;
	if (!IsHTTPHeaderComplete(HeaderBuffer, nTotal))
	{
		if (nTotal == 1023) 
		{	// the header is larger than the header buffer. TODO: deal with this case 
			return -1;
		}
		// get more data since there isn't a complete header yet
		while (!IsHTTPHeaderComplete(HeaderBuffer, nTotal) && (nTotal <= 1023) && (nRes == 0))
		{
			nRes = m_pSock->NBRead(HeaderBuffer + nTotal, 1023 - nTotal, &nBytes, nTimeout);
			nTotal += nBytes;
		}
		
		if ((nRes != 0) || (!IsHTTPHeaderComplete(HeaderBuffer, nTotal))) // socket error or bad header
		{
			return -1;
		}
	}
	
	// advance to the data now, if there is any in this first buffer. 
	char* pData = strstr(HeaderBuffer, "\r\n\r\n");
	if (pData) pData += 4;
	int nOffset = (int)pData - (int)HeaderBuffer;
	if (nTotal - nOffset >= nLen) // case 1: entire requested read is in header chunk
	{
		memcpy(pBuffer, pData, nLen);
		*nBytesWritten = nLen;
		
		// case 1: b. if excess data, store for the next read.
		if (nTotal > (nOffset + nLen))
		{
			m_nBufLen = nTotal - (nOffset + nLen);
			m_pTempBuf = new char[m_nBufLen]; // could clobber if non null already
			memcpy(m_pTempBuf, pData + nLen, m_nBufLen);
		}
		return 0;
	}
	else
	{	// case 2: entire requested read is NOT in header chunk
		memcpy(pBuffer, pData, nTotal - nOffset);
		nOffset = nTotal - nOffset;

		// only try one attempt to finish the read request
		nRes = m_pSock->NBRead(pBuffer + nOffset, nLen - nOffset, &nBytes, nTimeout);
		if (nRes != 0) return -1;	// socket error
		
		*nBytesWritten = nBytes + nOffset;
		return 0;
	}
}

/** Reads from a socket, into pbuffer, up to a max of nLen byte, and writes how many were actually written to nBytesWritten. */
int MBCOMHTTPSocket::Read(char* pBuffer, int nLen, int* nBytesWritten)
{
	if (!m_pSock->IsConnected()) return -1; // no connection
	int nOffset = 0;
	int nRes = 0;

	if (m_pTempBuf != NULL) // case 1: leftover bits from header stripping
	{
		if (m_nBufLen >= nLen)	// easy case: just copy from bit bucket and reduce its size
		{
			memcpy(pBuffer, m_pTempBuf, nLen);
			*nBytesWritten = nLen;
			if (nLen < m_nBufLen) // shift the bit bucket
			{
				memmove(m_pTempBuf, m_pTempBuf + nLen, m_nBufLen - nLen);
				m_nBufLen -= nLen;
			}
			else
			{
				delete [] m_pTempBuf;
				m_pTempBuf = NULL;
				m_nBufLen = 0;
			}
			return 0;	// success from bit bucket
		}
		else // bit bucket doesn't store it all
		{
			memcpy(pBuffer, m_pTempBuf, m_nBufLen);
			nOffset = m_nBufLen;
			*nBytesWritten = nOffset;
			
			delete [] m_pTempBuf;
			m_pTempBuf = NULL;
			m_nBufLen = 0;
		}
	}
	
	// general case: more bits are needed
	nRes = m_pSock->Read(pBuffer + nOffset, nLen - nOffset, nBytesWritten);
	*nBytesWritten += nOffset;
	
	return nRes;
}
 
/** Sets the proxy address. Use NULL to disable. */
int MBCOMHTTPSocket::SetProxy(const char* pURL)
{
	if (pURL == NULL) m_strProxyAddr = ""; // empty string
	else
	m_strProxyAddr = pURL;
	return 0;
}


bool MBCOMHTTPSocket::IsHTTPHeaderComplete(char* buffer, unsigned int length)
{
    bool result = false;

    for(char* cp = buffer; cp < buffer + length; cp++)
    {
        if(!strncmp(cp, "\n\n", 2) || !strncmp(cp, "\r\n\r\n", 4))
        {
            result = true;
            break;
        }
    }

    return result;
}