WorkingThread.cpp
上传用户:mingyuanzm
上传日期:2007-01-04
资源大小:117k
文件大小:21k
源码类别:

代理服务器

开发平台:

Visual C++

  1. /*
  2. * File name: WorkingThread.cpp
  3. * Version: 1.0.0.2
  4. * Programmer: A.George
  5. * Organization:
  6. * Date(start): 5.23.2000
  7. * Date(finish):
  8. * Description:
  9. Working thread.
  10. */
  11. #include "stdafx.h"
  12. #include "WorkingThread.h"
  13. #include "winsock.h"
  14. #include <stdio.h>
  15. #include <io.h>
  16. #include <afxinet.h>
  17. #define SITEBASE "g:\ag\personalproxy"
  18. /////////////////////////////////////////////////////////////////////////////
  19. int g_nConnectionCount;
  20. HANDLE g_hConnectionCount;
  21. unsigned long g_ulIpRefused[100];
  22. int g_nRefused;
  23. GLOBAL globalInfo;
  24. /////////////////////////////////////////////////////////////////////////////
  25. int (*ProcessCommand)(SOCKET sck, char* pszCommand, char* buf);
  26. /////////////////////////////////////////////////////////////////////////////
  27. BYTE Table(BYTE ot)
  28. {
  29. if(ot>=0 && ot<=25)return 'A' + ot;
  30. else if(ot>=26 && ot<=51)return 'a' + (ot-26);
  31. else if(ot>=52 && ot<=61)return '0' + (ot-52);
  32. else if(ot==62)return '+';
  33. else if(ot==63)return '/';
  34. else return '=';
  35. }
  36. void Base64(char* pszSource, char* pszCode)
  37. {
  38. BYTE ot;
  39. int i;
  40. i=0;
  41. while(pszSource[i])
  42. {
  43. ot = (0xFC & pszSource[i])>>2; *pszCode = Table(ot); i++; pszCode++;
  44. if(!pszSource[i])
  45. {
  46. ot = (0x03 & pszSource[i-1])<<4; *pszCode = Table(ot); pszCode++;
  47. break;
  48. }
  49. ot = (0x03 & pszSource[i-1])<<4 | (0xF0 & pszSource[i])>>4; *pszCode = Table(ot); pszCode++; i++;
  50. if(!pszSource[i])
  51. {
  52. ot = (0x0F & pszSource[i-1])<<2; *pszCode = Table(ot); pszCode++;
  53. break;
  54. }
  55. ot = (0x0F & pszSource[i-1])<<2 | (0xC0 & pszSource[i])>>6; *pszCode = Table(ot); pszCode++;
  56. ot = 0x3F & pszSource[i]; *pszCode = Table(ot), i++; pszCode++;
  57. }
  58. *pszCode = NULL;
  59. }
  60. BYTE GetSixBits(char* pszStr, int& i)
  61. {
  62. char c;
  63. while(1)
  64. {
  65. if(i>=strlen(pszStr))break;
  66. c = pszStr[i++];
  67. if(isupper(c)) return c-'A';
  68. else if(islower(c)) return c-'a'+26;
  69. else if(isdigit(c)) return c-'0'+52;
  70. else if(c=='+') return 62;
  71. else if(c=='/') return 63;
  72. else if(c=='=') return 0;
  73. }
  74. return 0;
  75. }
  76. void Unbase64(char* pszText, char* pszCode)
  77. {
  78. int nLen, i;
  79. BYTE bySixBits0, bySixBits1, bySixBits2, bySixBits3, c;
  80. nLen = strlen(pszCode);
  81. for(i=0; i<nLen; )
  82. {
  83. bySixBits0 = GetSixBits(pszCode, i);
  84. if(i<nLen)bySixBits1 = GetSixBits(pszCode, i);
  85. if(i<nLen)bySixBits2 = GetSixBits(pszCode, i);
  86. if(i<nLen)bySixBits3 = GetSixBits(pszCode, i);
  87. c = bySixBits0<<2 | bySixBits1>>4; *pszText = c; pszText++;
  88. c = bySixBits1<<4 | bySixBits2>>2; *pszText = c; pszText++;
  89. c = bySixBits2<<6 | bySixBits3; *pszText = c; pszText++;
  90. }
  91. *pszText = NULL;
  92. }
  93. DWORD RecvHeader(SOCKET socket, char *buf, DWORD dwSize, HTTPHEADER* pHeader)
  94. {
  95. char * pEnd, *pSize, *pType, *pLocation;
  96. int nRet, nTotal;
  97. memset(pHeader, 0, sizeof(HTTPHEADER));
  98. nTotal = 0;
  99. do{
  100. nRet = recv(socket, buf+nTotal, dwSize-nTotal, 0);
  101. if(nRet==SOCKET_ERROR)return SOCKET_ERROR;
  102. nTotal += nRet;
  103. buf[nRet] = NULL;
  104. pEnd = strstr(buf, "rnrn");
  105. if(pEnd)
  106. {
  107. pSize = strstr(buf, "Content-length: ");
  108. if(pSize)sscanf(pSize+16, "%ud", &(pHeader->dwContSize));
  109. else pHeader->dwContSize = -1;
  110. if(DWORD(pEnd-buf+4)<dwSize)
  111. pHeader->pContBegin = pEnd+4;
  112. else
  113. pHeader->pContBegin = NULL;
  114. sscanf(buf, "HTTP/%d.%d %d %s", &pHeader->nVerMajor, &pHeader->nVerMinor,
  115. &pHeader->dwHttpStatus, pHeader->pszStatus);
  116. pType = strstr(buf, "Content-type: ");
  117. if(pType)sscanf(pType+14, "%s", &(pHeader->pszType));
  118. pLocation = strstr(buf, "Location: ");
  119. if(pLocation)sscanf(pLocation+10, "s", &(pHeader->pszLocation));
  120. return nTotal;
  121. }
  122. }while(!pEnd && nRet);
  123. return nTotal;
  124. }
  125. int RecvRequest(SOCKET sck, char * buf, DWORD dwSize, REQUESTHEADER * pRequestHeader)
  126. {
  127. char * pEnd, pszCode[32], pszText[32], *pAuthorization, *pRange, *pSize;
  128. int iRet;
  129. DWORD nTotal;
  130. pRequestHeader->dwFlag = REQUEST_UNAUTHORIZED;
  131. memset(buf, 0, dwSize);
  132. nTotal = 0;
  133. do{
  134. iRet = recv(sck, buf+nTotal, dwSize-nTotal-1, 0);
  135. if(iRet==SOCKET_ERROR)
  136. {
  137. return SOCKET_ERROR;
  138. }
  139. nTotal += iRet;
  140. buf[nTotal] = NULL;
  141. pEnd = strstr(buf, "rnrn");
  142. if(pEnd)
  143. {
  144. pAuthorization = strstr(buf, "Proxy-Authorization: ");
  145. if(!pAuthorization)
  146. {
  147. pRequestHeader->pszUserPass[0] = NULL;
  148. return 0;
  149. }
  150. sscanf(pAuthorization+21, "%*s%s", pszCode);
  151. Unbase64(pszText, pszCode);
  152. if(strcmp(pszText, "user:ag"))
  153. {
  154. strcpy(pRequestHeader->pszUserPass, pszText);
  155. return 0;
  156. }
  157. else
  158. pRequestHeader->dwFlag = 0;
  159. pRequestHeader->pszAuthBegin = pAuthorization;
  160. pRequestHeader->pszAuthEnd = strstr(pAuthorization, "rn")+2;
  161. pRequestHeader->pszVirtualRes = strstr(buf, "GET "); //"http://www.personalproxy.com/");
  162. if(pRequestHeader->pszVirtualRes)
  163. {
  164. if(!strncmp(pRequestHeader->pszVirtualRes+4, "http://www.personalproxy.com", 28))
  165. {
  166. pRequestHeader->dwFlag |= VIRTUAL_RESOURCE;
  167. sscanf(pRequestHeader->pszVirtualRes+4, "%s", pRequestHeader->pszUrl);
  168. }
  169. }
  170. pRange = strstr(buf, "RANGE: bytes");
  171. if(pRange)
  172. {
  173. pRange = strstr(pRange, "=");
  174. if(pRange)
  175. {
  176. sscanf(pRange+1, "%d", &pRequestHeader->dwRange);
  177. pRequestHeader->dwFlag |= PARTIAL_CONTENT;
  178. }
  179. }
  180. pSize = strstr(buf, "Content-Length: ");
  181. if(pSize)
  182. {
  183. sscanf(pSize+16, "%d", &pRequestHeader->dwContSize);
  184. }
  185. else
  186. pRequestHeader->dwContSize = 0;
  187. pRequestHeader->pszContBegin = pEnd+4;
  188. return nTotal;
  189. }
  190. }while(!pEnd && iRet);
  191. return 0;
  192. }
  193. void UnCh(char* pszStr)
  194. {
  195. char pszLine[512];
  196. int i, j, nLen;
  197. BYTE b1, b2;
  198. i=0; j=0;
  199. nLen = strlen(pszStr);
  200. while(i<nLen)
  201. {
  202. if(pszStr[i]=='%')
  203. {
  204. i++;
  205. sscanf(pszStr+i, "%2X", &b1);
  206. i += 2;
  207. }
  208. else
  209. {
  210. pszLine[j++] = pszStr[i++];
  211. continue;
  212. }
  213. if(i>=nLen)
  214. {
  215. pszLine[j++] = b1;
  216. break;
  217. }
  218. if(pszStr[i]=='%')
  219. {
  220. i++;
  221. sscanf(pszStr+i, "%2X", &b2);
  222. i += 2;
  223. pszLine[j++] = b2;
  224. pszLine[j++] = b1;
  225. }
  226. else
  227. {
  228. pszLine[j++] = b1;
  229. pszLine[j++] = pszStr[i++];
  230. }
  231. }
  232. pszLine[j]=0;
  233. strcpy(pszStr, pszLine);
  234. }
  235. void ContentType(char* pszFile, char* pszType)
  236. {
  237. char drive[4], dir[MAX_PATH], fname[MAX_PATH], ext[16];
  238. _splitpath(pszFile, drive, dir, fname, ext);
  239. _strlwr(ext);
  240. if(strstr(pszFile, "multipart"))
  241. strcpy(pszType, "multipart/mixed");
  242. else if(!*ext)
  243. strcpy(pszType, "text/html");
  244. else if(!strcmp(ext, ".jpg"))
  245. strcpy(pszType, "image/jpeg");
  246. else if(!strcmp(ext, ".jpeg"))
  247. strcpy(pszType, "image/gif");
  248. else if(!strcmp(ext, ".htm"))
  249. strcpy(pszType, "text/html");
  250. else if(!strcmp(ext, ".html"))
  251. strcpy(pszType, "text/html");
  252. else if(!strcmp(ext, ".zip"))
  253. strcpy(pszType, "application/x-compressed");
  254. else if(!strcmp(ext, ".mpeg"))
  255. strcpy(pszType, "video/mpeg");
  256. else strcpy(pszType, "application/octet-stream");
  257. }
  258. void HttpService(char * pszUrl, SOCKET sck, char* buf, REQUESTHEADER* pRequestHeader)
  259. {
  260. CString strServer, strObject;
  261. DWORD dwService, dwSize, dwFileLen;
  262. unsigned short nPort;
  263. int iRet;
  264. char pszFile[512], pszLine[512], pszType[16];
  265. char pszCommand[128], *pQuestionMark;
  266. FILE* pFileContent;
  267. AfxParseURL(pszUrl, dwService, strServer, strObject, nPort);
  268. if(strObject=="/")
  269. sprintf(pszFile, "%s%s", SITEBASE, "\VirtualPage.html");
  270. else
  271. sprintf(pszFile, "%s%s", SITEBASE, strObject);
  272. UnCh(pszFile);
  273. // dwService 3
  274. // nPort 80
  275. // + strObject {"/count.dll?a=1&b=2"}
  276. // + strServer {"www.ms.com"}
  277. // + this 0x0012fe74 {CTryDlg hWnd=0x01460920}
  278. if(strstr(pszFile, "myextension"))
  279. {
  280. pQuestionMark = strstr(pszFile, "?");
  281. if(pQuestionMark)
  282. {
  283. strcpy(pszCommand, pQuestionMark+1);
  284. *pQuestionMark = NULL;
  285. }
  286. else
  287. * pszCommand = NULL;
  288. HMODULE hModule = LoadLibrary(pszFile);
  289. if(!hModule)
  290. {
  291. OUTPUT("requested file not foundrn");
  292. strcpy(buf, "HTTP/1.1 404 Not FoundrnTransfer-encoding: chunkedrnContent-type: text/htmlrnrn<HTML><H1>File not found</H1></HTML>");
  293. send(sck, buf, strlen(buf), 0);
  294. return;
  295. }
  296. ProcessCommand = (int(*)(unsigned int, char*, char*))GetProcAddress(hModule, "ProcessCommand");
  297. if(!ProcessCommand)
  298. {
  299. OUTPUT("ProcessCommand==NULLrn");
  300. FreeLibrary(hModule);
  301. return;
  302. }
  303. ProcessCommand(sck, pszCommand, buf);
  304. FreeLibrary(hModule);
  305. return;
  306. }
  307. pFileContent = fopen(pszFile, "rb");
  308. if(!pFileContent)
  309. {
  310. OUTPUT("requested file not foundrn");
  311. strcpy(buf, "HTTP/1.1 404 Not FoundrnTransfer-encoding: chunkedrnContent-type: text/htmlrnrn<HTML><H1>File not found</H1></HTML>");
  312. send(sck, buf, strlen(buf), 0);
  313. return;
  314. }
  315. dwFileLen = _filelength(_fileno(pFileContent));
  316. if(pRequestHeader->dwFlag & PARTIAL_CONTENT)
  317. {
  318. strcpy(buf, "HTTP/1.1 206 Partial Contentrn");
  319. strcat(buf, "Accept-ranges: bytesrn");
  320. dwSize = dwFileLen - pRequestHeader->dwRange;
  321. sprintf(pszLine, "Content-range: bytes %d-%d/%drn", pRequestHeader->dwRange, dwFileLen-1, dwFileLen);
  322. strcat(buf, pszLine);
  323. fseek(pFileContent, pRequestHeader->dwRange, SEEK_SET);
  324. }
  325. else
  326. {
  327. strcpy(buf, "HTTP/1.1 200 OKrn");
  328. dwSize = dwFileLen;
  329. }
  330. sprintf(pszLine, "Content-length: %drn", dwSize);
  331. strcat(buf, pszLine);
  332. strcpy(pszLine, "Proxy-agent: Ag-PersonalProxy/1.0rn");
  333. strcat(buf, pszLine);
  334. ContentType(pszFile, pszType);
  335. sprintf(pszLine, "Content-type: %srn", pszType);
  336. strcat(buf, pszLine);
  337. strcat(buf, "rn");
  338. send(sck, buf, strlen(buf), 0);
  339. while(!feof(pFileContent))
  340. {
  341. dwSize = fread(buf, 1, BUFFER_SIZE, pFileContent);
  342. iRet = send(sck, buf, dwSize, 0);
  343. if(iRet == SOCKET_ERROR)
  344. break;
  345. }
  346. fclose(pFileContent);
  347. return;
  348. }
  349. UINT WorkingThread(LPVOID lpVoid)
  350. {
  351. int iRet, i;
  352. char content[512], buf[BUFFER_SIZE], pszMsg[1024], pszCode[256], pszLine[512], *pEnd;
  353. WORKPARAM * pThreadParam = (WORKPARAM*)lpVoid;
  354. DWORD dwRet, dwTotal, dwSize;
  355. sockaddr_in addrProxy2nd;
  356. SOCKET sckClient;
  357. REQUESTHEADER requestHeader;
  358. HTTPHEADER httpHeader;
  359. FILE* pFileContent;
  360. dwRet = WaitForSingleObject(g_hConnectionCount, INFINITE);
  361. if(g_nConnectionCount>=80)
  362. {
  363. ReleaseSemaphore(g_hConnectionCount, 1, NULL);
  364. OUTPUT("Too many connections, limit reachedrn");
  365. strcpy(buf, "HTTP/1.1 431 Too many connections, limit reachedrn"); send(pThreadParam->sckClient , buf, strlen(buf), 0);
  366. strcpy(buf, "Proxy-agent: Ag-PersonalProxy/1.0rn"); send(pThreadParam->sckClient, buf, strlen(buf), 0);
  367. strcpy(buf, "Content-type: text/htmlrn"); send(pThreadParam->sckClient, buf, strlen(buf), 0);
  368. pFileContent = fopen("g:\ag\personalproxy\authorizationrequired.html", "rb");
  369. dwSize = _filelength(_fileno(pFileContent));
  370. sprintf(buf, "Content-length: %drn", dwSize); send(pThreadParam->sckClient, buf, strlen(buf), 0);
  371. strcpy(buf, "rn"); send(pThreadParam->sckClient, buf, strlen(buf), 0);
  372. while(!feof(pFileContent))
  373. {
  374. dwSize = fread(buf, 1, BUFFER_SIZE, pFileContent);
  375. send(pThreadParam->sckClient, buf, dwSize, 0);
  376. }
  377. closesocket(pThreadParam->sckClient);
  378. fclose(pFileContent);
  379. delete pThreadParam;
  380. return 0;
  381. }
  382. g_nConnectionCount++;
  383. ReleaseSemaphore(g_hConnectionCount, 1, NULL);
  384. while(1)
  385. {
  386. iRet = RecvRequest(pThreadParam->sckClient, buf, BUFFER_SIZE-1, &requestHeader);
  387. if(iRet==SOCKET_ERROR)
  388. {
  389. sprintf(pszMsg, "RecvRequest()=SOCKET_ERROR, WSAGetLastError()=%d", WSAGetLastError());
  390. OUTPUT(pszMsg);
  391. WaitForSingleObject(g_hConnectionCount, INFINITE);
  392. g_nConnectionCount--;
  393. ReleaseSemaphore(g_hConnectionCount, 1, NULL);
  394. delete pThreadParam;
  395. return 0;
  396. }
  397. dwTotal = iRet;
  398. for(i=0; i<g_nRefused; i++)
  399. {
  400. if(g_ulIpRefused[i]==pThreadParam->addr.sin_addr.S_un.S_addr)
  401. {
  402. OUTPUT("refused ip:");
  403. sprintf(content, "t%d.%d.%d.%drn",
  404. pThreadParam->addr.sin_addr.S_un.S_un_b.s_b1,
  405. pThreadParam->addr.sin_addr.S_un.S_un_b.s_b2,
  406. pThreadParam->addr.sin_addr.S_un.S_un_b.s_b3,
  407. pThreadParam->addr.sin_addr.S_un.S_un_b.s_b4);
  408. OUTPUT(content);
  409. strcpy(buf, "HTTP/1.1 432 Ip refusedrn"); send(pThreadParam->sckClient , buf, strlen(buf), 0);
  410. strcpy(buf, "Proxy-agent: Ag-PersonalProxy/1.0rn"); send(pThreadParam->sckClient, buf, strlen(buf), 0);
  411. strcpy(buf, "Content-type: text/htmlrn"); send(pThreadParam->sckClient, buf, strlen(buf), 0);
  412. pFileContent = fopen("g:\ag\personalproxy\notification.html", "rb");
  413. dwSize = _filelength(_fileno(pFileContent));
  414. sprintf(buf, "Content-length: %drn", dwSize); send(pThreadParam->sckClient, buf, strlen(buf), 0);
  415. strcpy(buf, "rn"); send(pThreadParam->sckClient, buf, strlen(buf), 0);
  416. dwSize = fread(buf, 1, BUFFER_SIZE, pFileContent);
  417. buf[dwSize] = NULL;
  418. sprintf(content, buf,
  419. pThreadParam->addr.sin_addr.S_un.S_un_b.s_b1,
  420. pThreadParam->addr.sin_addr.S_un.S_un_b.s_b2,
  421. pThreadParam->addr.sin_addr.S_un.S_un_b.s_b3,
  422. pThreadParam->addr.sin_addr.S_un.S_un_b.s_b4);
  423. send(pThreadParam->sckClient, content, strlen(content), 0);
  424. closesocket(pThreadParam->sckClient);
  425. fclose(pFileContent);
  426. goto _ThreadExit;
  427. }
  428. }
  429. if(requestHeader.dwFlag & REQUEST_UNAUTHORIZED)
  430. {
  431. OUTPUT("HTTP/1.1 407 Proxy authorization required agproxy.");
  432. sprintf(content, "t%st%d.%d.%d.%drn",
  433. requestHeader.pszUserPass,
  434. pThreadParam->addr.sin_addr.S_un.S_un_b.s_b1,
  435. pThreadParam->addr.sin_addr.S_un.S_un_b.s_b2,
  436. pThreadParam->addr.sin_addr.S_un.S_un_b.s_b3,
  437. pThreadParam->addr.sin_addr.S_un.S_un_b.s_b4);
  438. OUTPUT(content);
  439. //HTTP/1.0 407 Proxy authorization required
  440. strcpy(buf, "HTTP/1.1 407 Proxy authorization requiredrn"); send(pThreadParam->sckClient , buf, strlen(buf), 0);
  441. strcpy(buf, "Proxy-agent: Ag-PersonalProxy/1.0rn"); send(pThreadParam->sckClient, buf, strlen(buf), 0);
  442. strcpy(buf, "Content-type: image/jpegrn"); send(pThreadParam->sckClient, buf, strlen(buf), 0);
  443. strcpy(buf, "Proxy-authenticate: basic realm="JutAg"rn"); send(pThreadParam->sckClient, buf, strlen(buf), 0);
  444. pFileContent = fopen("g:\ag\personalproxy\Authorization.jpg", "rb");
  445. dwSize = _filelength(_fileno(pFileContent));
  446. sprintf(buf, "Content-length: %drn", dwSize); send(pThreadParam->sckClient, buf, strlen(buf), 0);
  447. strcpy(buf, "rn"); send(pThreadParam->sckClient, buf, strlen(buf), 0);
  448. while(!feof(pFileContent))
  449. {
  450. dwSize = fread(buf, 1, BUFFER_SIZE, pFileContent);
  451. send(pThreadParam->sckClient, buf, dwSize, 0);
  452. }
  453. fclose(pFileContent);
  454. continue;
  455. }
  456. break;
  457. }
  458. if(strstr(buf, "POST"))
  459. {
  460. OUTPUT(buf);
  461. OUTPUT("rn");
  462. }
  463. else
  464. {
  465. strncpy(content, buf, strstr(buf, "rn")-buf+2);
  466. content[strstr(buf, "rn")-buf] = NULL;
  467. OUTPUT(content);
  468. }
  469. sprintf(content, "t%d.%d.%d.%drn",
  470. pThreadParam->addr.sin_addr.S_un.S_un_b.s_b1,
  471. pThreadParam->addr.sin_addr.S_un.S_un_b.s_b2,
  472. pThreadParam->addr.sin_addr.S_un.S_un_b.s_b3,
  473. pThreadParam->addr.sin_addr.S_un.S_un_b.s_b4);
  474. OUTPUT(content);
  475. if(requestHeader.dwFlag & VIRTUAL_RESOURCE)
  476. {
  477. HttpService(requestHeader.pszUrl, pThreadParam->sckClient, buf, &requestHeader);
  478. goto _ThreadExit;
  479. }
  480. OUTPUT("Connecting to the second proxy.rn");
  481. addrProxy2nd.sin_family = AF_INET;
  482. addrProxy2nd.sin_port = htons(globalInfo.nPort2nd);
  483. addrProxy2nd.sin_addr.S_un.S_un_b.s_b1 = globalInfo.b1;
  484. addrProxy2nd.sin_addr.S_un.S_un_b.s_b2 = globalInfo.b2;
  485. addrProxy2nd.sin_addr.S_un.S_un_b.s_b3 = globalInfo.b3;
  486. addrProxy2nd.sin_addr.S_un.S_un_b.s_b4 = globalInfo.b4;
  487. sckClient = socket(AF_INET, SOCK_STREAM, 0);
  488. if(sckClient==INVALID_SOCKET)
  489. {
  490. closesocket(pThreadParam->sckClient);
  491. closesocket(sckClient);
  492. sprintf(pszMsg, "socket()=INVALID_SOCKET, WSAGetLastError()=%drnListening Thread terminated.rn", WSAGetLastError());
  493. OUTPUT(pszMsg);
  494. goto _ThreadExit1;
  495. }
  496. iRet = connect(sckClient, (const sockaddr*)&addrProxy2nd, sizeof(addrProxy2nd));
  497. if(iRet==SOCKET_ERROR)
  498. {
  499. closesocket(pThreadParam->sckClient);
  500. closesocket(sckClient);
  501. sprintf(pszMsg, "connect()=SOCKET_ERROR, WSAGetLastError()=%drnListening Thread terminated.rn", WSAGetLastError());
  502. OUTPUT(pszMsg);
  503. goto _ThreadExit1;
  504. }
  505. // send the data before Proxy-Authorization:
  506. send(sckClient, buf, requestHeader.pszAuthBegin-buf, 0);
  507. strncpy(pszMsg, buf, requestHeader.pszAuthBegin-buf);
  508. pszMsg[requestHeader.pszAuthBegin-buf] = NULL;
  509. // TRACE(pszMsg);
  510. // send Authorization information to the second proxy
  511. sprintf(pszMsg, "%s:%s", globalInfo.pszUser, globalInfo.pszPasswd);
  512. Base64(pszMsg, pszCode);
  513. sprintf(content, "Proxy-Authorization: Basic %s==rn", pszCode);
  514. send(sckClient, content, strlen(content), 0);
  515. // TRACE(content);
  516. // send(sckClient, requestHeader.pszAuthBegin, requestHeader.pszAuthEnd-requestHeader.pszAuthBegin, 0);
  517. strncpy(pszMsg, requestHeader.pszAuthBegin, requestHeader.pszAuthEnd-requestHeader.pszAuthBegin);
  518. pszMsg[requestHeader.pszAuthBegin, requestHeader.pszAuthEnd-requestHeader.pszAuthBegin] = NULL;
  519. // TRACE(pszMsg);
  520. // send the rest information
  521. send(sckClient, requestHeader.pszAuthEnd, strlen(requestHeader.pszAuthEnd), 0);
  522. iRet = 1;
  523. while(dwTotal<requestHeader.dwContSize && iRet)
  524. {
  525. iRet = recv(pThreadParam->sckClient, buf, BUFFER_SIZE, 0);
  526. buf[iRet] = NULL;
  527. if(iRet==SOCKET_ERROR)
  528. {
  529. sprintf(pszMsg, "while(nTotal<requestHeader.dwContSize && iRet) recv()=SOCKET_ERROR, WSAGetLastError()=%drnListening Thread terminated.rn", WSAGetLastError());
  530. OUTPUT(pszMsg);
  531. goto _ThreadExit;
  532. }
  533. if(iRet)
  534. {
  535. OUTPUT("_DEBUG_PRE_t");OUTPUT(buf);
  536. send(sckClient, buf, iRet, 0);
  537. dwTotal += iRet;
  538. }
  539. }
  540. // TRACE(requestHeader.pszAuthEnd);
  541. // waiting for response from server
  542. iRet = RecvHeader(sckClient, buf, BUFFER_SIZE, &httpHeader);
  543. send(pThreadParam->sckClient, buf, iRet, 0);
  544. if(httpHeader.dwContSize==0)
  545. {
  546. goto _ThreadExit;
  547. }
  548. dwTotal = iRet-(httpHeader.pContBegin - buf);
  549. while(dwTotal<httpHeader.dwContSize && iRet)
  550. {
  551. iRet = recv(sckClient, buf, BUFFER_SIZE, 0);
  552. if(iRet==SOCKET_ERROR)
  553. {
  554. sprintf(pszMsg, "recv()=SOCKET_ERROR, WSAGetLastError()=%drnListening Thread terminated.rn", WSAGetLastError());
  555. OUTPUT(pszMsg);
  556. goto _ThreadExit;
  557. }
  558. dwTotal += iRet;
  559. send(pThreadParam->sckClient, buf, iRet, 0);
  560. }
  561. _ThreadExit:
  562. OUTPUT("Working thread terminated.rn");
  563. closesocket(sckClient);
  564. closesocket(pThreadParam->sckClient);
  565. _ThreadExit1:
  566. WaitForSingleObject(g_hConnectionCount, INFINITE);
  567. g_nConnectionCount--;
  568. ReleaseSemaphore(g_hConnectionCount, 1, NULL);
  569. delete pThreadParam;
  570. return 0;
  571. }
  572. UINT ListeningThread(LPVOID lpVoid)
  573. {
  574. int iRet, addrLen;
  575. sockaddr_in addr, addrAccept;
  576. SOCKET sckListen, sckAccept;
  577. char pszMsg[512];
  578. int nErrCount;
  579. hostent * pEnt;
  580. WORKPARAM * pWorkParam;
  581. OUTPUT("going to create socket.rn");
  582. sckListen = socket(AF_INET, SOCK_STREAM, 0);
  583. if(sckListen==INVALID_SOCKET)
  584. {
  585. sprintf(pszMsg, "INVALID_SOCKET, WSAGetLastError()=%drnListening Thread terminated.rn", WSAGetLastError());
  586. OUTPUT(pszMsg);
  587. return 0;
  588. }
  589. OUTPUT("Listening socket created successfully.rn");
  590. iRet = gethostname(pszMsg, 256);
  591. if(iRet)
  592. {
  593. sprintf(pszMsg, "Error occur when gethostname(), WSAGetLastError()=%drnListening Thread terminated.rn", WSAGetLastError());
  594. OUTPUT(pszMsg);
  595. return 0;
  596. }
  597. pEnt =gethostbyname(pszMsg);
  598. if(!pEnt)
  599. {
  600. sprintf(pszMsg, "Error occur when gethostbyname(), WSAGetLastError()=%drnListening Thread terminated.rn", WSAGetLastError());
  601. OUTPUT(pszMsg);
  602. return 0;
  603. }
  604. addr.sin_family = AF_INET;
  605. addr.sin_port = htons(globalInfo.nPort);
  606. addr.sin_addr.S_un.S_un_b.s_b1 = pEnt->h_addr_list[0][0];
  607. addr.sin_addr.S_un.S_un_b.s_b2 = pEnt->h_addr_list[0][1];
  608. addr.sin_addr.S_un.S_un_b.s_b3 = pEnt->h_addr_list[0][2];
  609. addr.sin_addr.S_un.S_un_b.s_b4 = pEnt->h_addr_list[0][3];
  610. globalInfo._b1 = pEnt->h_addr_list[0][0];
  611. globalInfo._b2 = pEnt->h_addr_list[0][1];
  612. globalInfo._b3 = pEnt->h_addr_list[0][2];
  613. globalInfo._b4 = pEnt->h_addr_list[0][3];
  614. iRet = bind(sckListen, (const sockaddr*)&addr, sizeof(addr));
  615. if(iRet==SOCKET_ERROR)
  616. {
  617. sprintf(pszMsg, "SOCKET_ERROR, WSAGetLastError()=%d", WSAGetLastError());
  618. OUTPUT(pszMsg);
  619. return 0;
  620. }
  621. nErrCount = 0;
  622. OUTPUT("Listening...rn");
  623. iRet = listen(sckListen, SOMAXCONN);
  624. if(iRet==SOCKET_ERROR)
  625. {
  626. sprintf(pszMsg, "INVALID_SOCKET, WSAGetLastError()=%d", WSAGetLastError());
  627. OUTPUT(pszMsg);
  628. nErrCount++;
  629. if(nErrCount>=10)
  630. {
  631. sprintf(pszMsg, "nErrCount>=10, listening thread terminated.rn");
  632. return 0;
  633. }
  634. }
  635. nErrCount = 0;
  636. while(1)
  637. {
  638. addrLen = sizeof(addrAccept);
  639. sckAccept = accept(sckListen, (struct sockaddr*)&addrAccept, &addrLen);
  640. if(sckAccept==INVALID_SOCKET)
  641. {
  642. sprintf(pszMsg, "accept()=SOCKET_ERROR, WSAGetLastError()=%d", WSAGetLastError());
  643. OUTPUT(pszMsg);
  644. return 0;
  645. }
  646. OUTPUT("One client accepted successfully.rn");
  647. pWorkParam = new WORKPARAM;
  648. pWorkParam->sckClient = sckAccept;
  649. pWorkParam->addr = addrAccept;
  650. AfxBeginThread(WorkingThread, (LPVOID)pWorkParam, THREAD_PRIORITY_BELOW_NORMAL);
  651. }
  652. return 0;
  653. }