PageRenderTime 59ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/sapi/litespeed/lsapilib.c

http://github.com/infusion/PHP
C | 2227 lines | 1894 code | 269 blank | 64 comment | 425 complexity | 76735ccfc98e061cc53f14e1544617b2 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2007 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available at through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt. |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: George Wang <gwang@litespeedtech.com> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id: lsapilib.c 306212 2010-12-10 22:51:08Z gwang $ */
  19. /*
  20. Copyright (c) 2007, Lite Speed Technologies Inc.
  21. All rights reserved.
  22. Redistribution and use in source and binary forms, with or without
  23. modification, are permitted provided that the following conditions are
  24. met:
  25. * Redistributions of source code must retain the above copyright
  26. notice, this list of conditions and the following disclaimer.
  27. * Redistributions in binary form must reproduce the above
  28. copyright notice, this list of conditions and the following
  29. disclaimer in the documentation and/or other materials provided
  30. with the distribution.
  31. * Neither the name of the Lite Speed Technologies Inc nor the
  32. names of its contributors may be used to endorse or promote
  33. products derived from this software without specific prior
  34. written permission.
  35. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  36. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  37. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  38. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  39. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  42. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  43. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  45. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. */
  47. #include <lsapilib.h>
  48. #include <ctype.h>
  49. #include <errno.h>
  50. #include <fcntl.h>
  51. #include <arpa/inet.h>
  52. #include <netdb.h>
  53. #include <netinet/in.h>
  54. #include <netinet/tcp.h>
  55. #include <sys/un.h>
  56. #include <signal.h>
  57. #include <stdlib.h>
  58. #include <stdio.h>
  59. #include <string.h>
  60. #include <sys/mman.h>
  61. #include <sys/resource.h>
  62. #include <sys/socket.h>
  63. #include <sys/time.h>
  64. #include <sys/uio.h>
  65. #include <sys/wait.h>
  66. #include <time.h>
  67. #include <unistd.h>
  68. #define LSAPI_ST_REQ_HEADER 1
  69. #define LSAPI_ST_REQ_BODY 2
  70. #define LSAPI_ST_RESP_HEADER 4
  71. #define LSAPI_ST_RESP_BODY 8
  72. #define LSAPI_RESP_BUF_SIZE 8192
  73. #define LSAPI_INIT_RESP_HEADER_LEN 4096
  74. static int g_inited = 0;
  75. static int g_running = 1;
  76. static int s_ppid;
  77. static int s_slow_req_msecs = 0;
  78. LSAPI_Request g_req = { -1, -1 };
  79. void Flush_RespBuf_r( LSAPI_Request * pReq );
  80. static const char *CGI_HEADERS[H_TRANSFER_ENCODING+1] =
  81. {
  82. "HTTP_ACCEPT", "HTTP_ACCEPT_CHARSET",
  83. "HTTP_ACCEPT_ENCODING",
  84. "HTTP_ACCEPT_LANGUAGE", "HTTP_AUTHORIZATION",
  85. "HTTP_CONNECTION", "CONTENT_TYPE",
  86. "CONTENT_LENGTH", "HTTP_COOKIE", "HTTP_COOKIE2",
  87. "HTTP_HOST", "HTTP_PRAGMA",
  88. "HTTP_REFERER", "HTTP_USER_AGENT",
  89. "HTTP_CACHE_CONTROL",
  90. "HTTP_IF_MODIFIED_SINCE", "HTTP_IF_MATCH",
  91. "HTTP_IF_NONE_MATCH",
  92. "HTTP_IF_RANGE",
  93. "HTTP_IF_UNMODIFIED_SINCE",
  94. "HTTP_KEEP_ALIVE",
  95. "HTTP_RANGE",
  96. "HTTP_X_FORWARDED_FOR",
  97. "HTTP_VIA",
  98. "HTTP_TRANSFER_ENCODING"
  99. };
  100. static int CGI_HEADER_LEN[H_TRANSFER_ENCODING+1] = {
  101. 11, 19, 20, 20, 18, 15, 12, 14, 11, 12, 9, 11, 12, 15, 18,
  102. 22, 13, 18, 13, 24, 15, 10, 20, 8, 22
  103. };
  104. static const char *HTTP_HEADERS[H_TRANSFER_ENCODING+1] = {
  105. "Accept", "Accept-Charset",
  106. "Accept-Encoding",
  107. "Accept-Language", "Authorization",
  108. "Connection", "Content-Type",
  109. "Content-Length", "Cookie", "Cookie2",
  110. "Host", "Pragma",
  111. "Referer", "User-Agent",
  112. "Cache-Control",
  113. "If-Modified-Since", "If-Match",
  114. "If-None-Match",
  115. "If-Range",
  116. "If-Unmodified-Since",
  117. "Keep-Alive",
  118. "Range",
  119. "X-Forwarded-For",
  120. "Via",
  121. "Transfer-Encoding"
  122. };
  123. static int HTTP_HEADER_LEN[H_TRANSFER_ENCODING+1] = {
  124. 6, 14, 15, 15, 13, 10, 12, 14, 6, 7, 4, 6, 7, 10, /* user-agent */
  125. 13,17, 8, 13, 8, 19, 10, 5, 15, 3, 17
  126. };
  127. static void lsapi_sigpipe( int sig )
  128. {
  129. }
  130. static void lsapi_siguser1( int sig )
  131. {
  132. g_running = 0;
  133. }
  134. #ifndef sighandler_t
  135. typedef void (*sighandler_t)(int);
  136. #endif
  137. static void lsapi_signal(int signo, sighandler_t handler)
  138. {
  139. struct sigaction sa;
  140. sigaction(signo, NULL, &sa);
  141. if (sa.sa_handler == SIG_DFL) {
  142. sigemptyset(&sa.sa_mask);
  143. sa.sa_flags = 0;
  144. sa.sa_handler = handler;
  145. sigaction(signo, &sa, NULL);
  146. }
  147. }
  148. static inline void lsapi_buildPacketHeader( struct lsapi_packet_header * pHeader,
  149. char type, int len )
  150. {
  151. pHeader->m_versionB0 = LSAPI_VERSION_B0; /* LSAPI protocol version */
  152. pHeader->m_versionB1 = LSAPI_VERSION_B1;
  153. pHeader->m_type = type;
  154. pHeader->m_flag = LSAPI_ENDIAN;
  155. pHeader->m_packetLen.m_iLen = len;
  156. }
  157. static int lsapi_set_nblock( int fd, int nonblock )
  158. {
  159. int val = fcntl( fd, F_GETFL, 0 );
  160. if ( nonblock )
  161. {
  162. if (!( val & O_NONBLOCK ))
  163. {
  164. return fcntl( fd, F_SETFL, val | O_NONBLOCK );
  165. }
  166. }
  167. else
  168. {
  169. if ( val & O_NONBLOCK )
  170. {
  171. return fcntl( fd, F_SETFL, val &(~O_NONBLOCK) );
  172. }
  173. }
  174. return 0;
  175. }
  176. static int lsapi_close( int fd )
  177. {
  178. int ret;
  179. while( 1 ) {
  180. ret = close( fd );
  181. if (( ret == -1 )&&( errno == EINTR )&&(g_running)) {
  182. continue;
  183. }
  184. return ret;
  185. }
  186. }
  187. static inline int lsapi_read( int fd, void * pBuf, int len )
  188. {
  189. int ret;
  190. while( 1 ) {
  191. ret = read( fd, (char *)pBuf, len );
  192. if (( ret == -1 )&&( errno == EINTR )&&(g_running)) {
  193. continue;
  194. }
  195. return ret;
  196. }
  197. }
  198. static int lsapi_writev( int fd, struct iovec ** pVec, int count, int totalLen )
  199. {
  200. int ret;
  201. int left = totalLen;
  202. int n = count;
  203. while(( left > 0 )&&g_running ) {
  204. ret = writev( fd, *pVec, n );
  205. if ( ret > 0 ) {
  206. left -= ret;
  207. if (( left <= 0)||( !g_running )) {
  208. return totalLen - left;
  209. }
  210. while( ret > 0 ) {
  211. if ( (*pVec)->iov_len <= ret ) {
  212. ret -= (*pVec)->iov_len;
  213. ++(*pVec);
  214. } else {
  215. (*pVec)->iov_base = (char *)(*pVec)->iov_base + ret;
  216. (*pVec)->iov_len -= ret;
  217. break;
  218. }
  219. }
  220. } else if ( ret == -1 ) {
  221. if ( errno == EAGAIN ) {
  222. if ( totalLen - left > 0 ) {
  223. return totalLen - left;
  224. } else {
  225. return -1;
  226. }
  227. } else {
  228. if ( errno != EINTR ) {
  229. return ret;
  230. }
  231. }
  232. }
  233. }
  234. return totalLen - left;
  235. }
  236. static inline int allocateBuf( LSAPI_Request * pReq, int size )
  237. {
  238. char * pBuf = (char *)realloc( pReq->m_pReqBuf, size );
  239. if ( pBuf ) {
  240. pReq->m_pReqBuf = pBuf;
  241. pReq->m_reqBufSize = size;
  242. pReq->m_pHeader = (struct lsapi_req_header *)pReq->m_pReqBuf;
  243. return 0;
  244. }
  245. return -1;
  246. }
  247. static int allocateIovec( LSAPI_Request * pReq, int n )
  248. {
  249. struct iovec * p = (struct iovec *)realloc(
  250. pReq->m_pIovec, sizeof(struct iovec) * n );
  251. if ( !p ) {
  252. return -1;
  253. }
  254. pReq->m_pIovecToWrite = p + ( pReq->m_pIovecToWrite - pReq->m_pIovec );
  255. pReq->m_pIovecCur = p + ( pReq->m_pIovecCur - pReq->m_pIovec );
  256. pReq->m_pIovec = p;
  257. pReq->m_pIovecEnd = p + n;
  258. return 0;
  259. }
  260. static int allocateRespHeaderBuf( LSAPI_Request * pReq, int size )
  261. {
  262. char * p = (char *)realloc( pReq->m_pRespHeaderBuf, size );
  263. if ( !p ) {
  264. return -1;
  265. }
  266. pReq->m_pRespHeaderBufPos = p + ( pReq->m_pRespHeaderBufPos - pReq->m_pRespHeaderBuf );
  267. pReq->m_pRespHeaderBuf = p;
  268. pReq->m_pRespHeaderBufEnd = p + size;
  269. return 0;
  270. }
  271. static inline int verifyHeader( struct lsapi_packet_header * pHeader, char pktType )
  272. {
  273. if (( LSAPI_VERSION_B0 != pHeader->m_versionB0 )||
  274. ( LSAPI_VERSION_B1 != pHeader->m_versionB1 )||
  275. ( pktType != pHeader->m_type )) {
  276. return -1;
  277. }
  278. if ( LSAPI_ENDIAN != (pHeader->m_flag & LSAPI_ENDIAN_BIT )) {
  279. register char b;
  280. b = pHeader->m_packetLen.m_bytes[0];
  281. pHeader->m_packetLen.m_bytes[0] = pHeader->m_packetLen.m_bytes[3];
  282. pHeader->m_packetLen.m_bytes[3] = b;
  283. b = pHeader->m_packetLen.m_bytes[1];
  284. pHeader->m_packetLen.m_bytes[1] = pHeader->m_packetLen.m_bytes[2];
  285. pHeader->m_packetLen.m_bytes[2] = b;
  286. }
  287. return pHeader->m_packetLen.m_iLen;
  288. }
  289. static int allocateEnvList( struct LSAPI_key_value_pair ** pEnvList,
  290. int *curSize, int newSize )
  291. {
  292. struct LSAPI_key_value_pair * pBuf;
  293. if ( *curSize >= newSize ) {
  294. return 0;
  295. }
  296. if ( newSize > 8192 ) {
  297. return -1;
  298. }
  299. pBuf = (struct LSAPI_key_value_pair *)realloc( *pEnvList, newSize *
  300. sizeof(struct LSAPI_key_value_pair) );
  301. if ( pBuf ) {
  302. *pEnvList = pBuf;
  303. *curSize = newSize;
  304. return 0;
  305. } else {
  306. return -1;
  307. }
  308. }
  309. static inline int isPipe( int fd )
  310. {
  311. char achPeer[128];
  312. socklen_t len = 128;
  313. if (( getpeername( fd, (struct sockaddr *)achPeer, &len ) != 0 )&&
  314. ( errno == ENOTCONN )) {
  315. return 0;
  316. } else {
  317. return 1;
  318. }
  319. }
  320. static int parseEnv( struct LSAPI_key_value_pair * pEnvList, int count,
  321. char **pBegin, char * pEnd )
  322. {
  323. struct LSAPI_key_value_pair * pEnvEnd;
  324. int keyLen = 0, valLen = 0;
  325. if ( count > 8192 ) {
  326. return -1;
  327. }
  328. pEnvEnd = pEnvList + count;
  329. while( pEnvList != pEnvEnd ) {
  330. if ( pEnd - *pBegin < 4 ) {
  331. return -1;
  332. }
  333. keyLen = *((unsigned char *)((*pBegin)++));
  334. keyLen = (keyLen << 8) + *((unsigned char *)((*pBegin)++));
  335. valLen = *((unsigned char *)((*pBegin)++));
  336. valLen = (valLen << 8) + *((unsigned char *)((*pBegin)++));
  337. if ( *pBegin + keyLen + valLen > pEnd ) {
  338. return -1;
  339. }
  340. if (( !keyLen )||( !valLen )) {
  341. return -1;
  342. }
  343. pEnvList->pKey = *pBegin;
  344. *pBegin += keyLen;
  345. pEnvList->pValue = *pBegin;
  346. *pBegin += valLen;
  347. pEnvList->keyLen = keyLen - 1;
  348. pEnvList->valLen = valLen - 1;
  349. ++pEnvList;
  350. }
  351. if ( memcmp( *pBegin, "\0\0\0\0", 4 ) != 0 ) {
  352. return -1;
  353. }
  354. *pBegin += 4;
  355. return 0;
  356. }
  357. static inline void swapIntEndian( int * pInteger )
  358. {
  359. char * p = (char *)pInteger;
  360. register char b;
  361. b = p[0];
  362. p[0] = p[3];
  363. p[3] = b;
  364. b = p[1];
  365. p[1] = p[2];
  366. p[2] = b;
  367. }
  368. static inline void fixEndian( LSAPI_Request * pReq )
  369. {
  370. struct lsapi_req_header *p= pReq->m_pHeader;
  371. swapIntEndian( &p->m_httpHeaderLen );
  372. swapIntEndian( &p->m_reqBodyLen );
  373. swapIntEndian( &p->m_scriptFileOff );
  374. swapIntEndian( &p->m_scriptNameOff );
  375. swapIntEndian( &p->m_queryStringOff );
  376. swapIntEndian( &p->m_requestMethodOff );
  377. swapIntEndian( &p->m_cntUnknownHeaders );
  378. swapIntEndian( &p->m_cntEnv );
  379. swapIntEndian( &p->m_cntSpecialEnv );
  380. }
  381. static void fixHeaderIndexEndian( LSAPI_Request * pReq )
  382. {
  383. int i;
  384. for( i = 0; i < H_TRANSFER_ENCODING; ++i ) {
  385. if ( pReq->m_pHeaderIndex->m_headerOff[i] ) {
  386. register char b;
  387. char * p = (char *)(&pReq->m_pHeaderIndex->m_headerLen[i]);
  388. b = p[0];
  389. p[0] = p[1];
  390. p[1] = b;
  391. swapIntEndian( &pReq->m_pHeaderIndex->m_headerOff[i] );
  392. }
  393. }
  394. if ( pReq->m_pHeader->m_cntUnknownHeaders > 0 ) {
  395. struct lsapi_header_offset * pCur, *pEnd;
  396. pCur = pReq->m_pUnknownHeader;
  397. pEnd = pCur + pReq->m_pHeader->m_cntUnknownHeaders;
  398. while( pCur < pEnd ) {
  399. swapIntEndian( &pCur->nameOff );
  400. swapIntEndian( &pCur->nameLen );
  401. swapIntEndian( &pCur->valueOff );
  402. swapIntEndian( &pCur->valueLen );
  403. ++pCur;
  404. }
  405. }
  406. }
  407. static int parseRequest( LSAPI_Request * pReq, int totalLen )
  408. {
  409. int shouldFixEndian;
  410. char * pBegin = pReq->m_pReqBuf + sizeof( struct lsapi_req_header );
  411. char * pEnd = pReq->m_pReqBuf + totalLen;
  412. shouldFixEndian = ( LSAPI_ENDIAN != (
  413. pReq->m_pHeader->m_pktHeader.m_flag & LSAPI_ENDIAN_BIT ) );
  414. if ( shouldFixEndian ) {
  415. fixEndian( pReq );
  416. }
  417. if ( (pReq->m_specialEnvListSize < pReq->m_pHeader->m_cntSpecialEnv )&&
  418. allocateEnvList( &pReq->m_pSpecialEnvList,
  419. &pReq->m_specialEnvListSize,
  420. pReq->m_pHeader->m_cntSpecialEnv ) == -1 ) {
  421. return -1;
  422. }
  423. if ( (pReq->m_envListSize < pReq->m_pHeader->m_cntEnv )&&
  424. allocateEnvList( &pReq->m_pEnvList, &pReq->m_envListSize,
  425. pReq->m_pHeader->m_cntEnv ) == -1 ) {
  426. return -1;
  427. }
  428. if ( parseEnv( pReq->m_pSpecialEnvList,
  429. pReq->m_pHeader->m_cntSpecialEnv,
  430. &pBegin, pEnd ) == -1 ) {
  431. return -1;
  432. }
  433. if ( parseEnv( pReq->m_pEnvList, pReq->m_pHeader->m_cntEnv,
  434. &pBegin, pEnd ) == -1 ) {
  435. return -1;
  436. }
  437. pReq->m_pScriptFile = pReq->m_pReqBuf + pReq->m_pHeader->m_scriptFileOff;
  438. pReq->m_pScriptName = pReq->m_pReqBuf + pReq->m_pHeader->m_scriptNameOff;
  439. pReq->m_pQueryString = pReq->m_pReqBuf + pReq->m_pHeader->m_queryStringOff;
  440. pReq->m_pRequestMethod = pReq->m_pReqBuf + pReq->m_pHeader->m_requestMethodOff;
  441. pBegin = pReq->m_pReqBuf + (( pBegin - pReq->m_pReqBuf + 7 ) & (~0x7));
  442. pReq->m_pHeaderIndex = ( struct lsapi_http_header_index * )pBegin;
  443. pBegin += sizeof( struct lsapi_http_header_index );
  444. pReq->m_pUnknownHeader = (struct lsapi_header_offset *)pBegin;
  445. pBegin += sizeof( struct lsapi_header_offset) *
  446. pReq->m_pHeader->m_cntUnknownHeaders;
  447. pReq->m_pHttpHeader = pBegin;
  448. pBegin += pReq->m_pHeader->m_httpHeaderLen;
  449. if ( pBegin != pEnd ) {
  450. return -1;
  451. }
  452. if ( shouldFixEndian ) {
  453. fixHeaderIndexEndian( pReq );
  454. }
  455. return 0;
  456. }
  457. static int s_accept_notify = 0;
  458. static struct lsapi_packet_header ack = {'L', 'S',
  459. LSAPI_REQ_RECEIVED, LSAPI_ENDIAN, {LSAPI_PACKET_HEADER_LEN} };
  460. static inline int notify_req_received( int fd )
  461. {
  462. if ( write( fd, &ack, LSAPI_PACKET_HEADER_LEN )
  463. < LSAPI_PACKET_HEADER_LEN ) {
  464. return -1;
  465. }
  466. return 0;
  467. }
  468. static int readReq( LSAPI_Request * pReq )
  469. {
  470. int len;
  471. int packetLen;
  472. if ( !pReq ) {
  473. return -1;
  474. }
  475. if ( pReq->m_reqBufSize < 8192 ) {
  476. if ( allocateBuf( pReq, 8192 ) == -1 ) {
  477. return -1;
  478. }
  479. }
  480. while ( pReq->m_bufRead < LSAPI_PACKET_HEADER_LEN ) {
  481. len = lsapi_read( pReq->m_fd, pReq->m_pReqBuf, pReq->m_reqBufSize );
  482. if ( len <= 0 ) {
  483. return -1;
  484. }
  485. pReq->m_bufRead += len;
  486. }
  487. pReq->m_reqState = LSAPI_ST_REQ_HEADER;
  488. packetLen = verifyHeader( &pReq->m_pHeader->m_pktHeader, LSAPI_BEGIN_REQUEST );
  489. if ( packetLen < 0 ) {
  490. return -1;
  491. }
  492. if ( packetLen > LSAPI_MAX_HEADER_LEN ) {
  493. return -1;
  494. }
  495. if ( packetLen + 1024 > pReq->m_reqBufSize ) {
  496. if ( allocateBuf( pReq, packetLen + 1024 ) == -1 ) {
  497. return -1;
  498. }
  499. }
  500. while( packetLen > pReq->m_bufRead ) {
  501. len = lsapi_read( pReq->m_fd, pReq->m_pReqBuf + pReq->m_bufRead, packetLen - pReq->m_bufRead );
  502. if ( len <= 0 ) {
  503. return -1;
  504. }
  505. pReq->m_bufRead += len;
  506. }
  507. if ( parseRequest( pReq, packetLen ) < 0 ) {
  508. return -1;
  509. }
  510. pReq->m_bufProcessed = packetLen;
  511. pReq->m_reqState = LSAPI_ST_REQ_BODY | LSAPI_ST_RESP_HEADER;
  512. if ( !s_accept_notify )
  513. return notify_req_received( pReq->m_fd );
  514. else
  515. return 0;
  516. }
  517. int LSAPI_Init(void)
  518. {
  519. if ( !g_inited ) {
  520. lsapi_signal(SIGPIPE, lsapi_sigpipe);
  521. lsapi_signal(SIGUSR1, lsapi_siguser1);
  522. #if defined(SIGXFSZ) && defined(SIG_IGN)
  523. signal(SIGXFSZ, SIG_IGN);
  524. #endif
  525. /* let STDOUT function as STDERR,
  526. just in case writing to STDOUT directly */
  527. dup2( 2, 1 );
  528. if ( LSAPI_InitRequest( &g_req, LSAPI_SOCK_FILENO ) == -1 ) {
  529. return -1;
  530. }
  531. g_inited = 1;
  532. s_ppid = getppid();
  533. }
  534. return 0;
  535. }
  536. void LSAPI_Stop(void)
  537. {
  538. g_running = 0;
  539. }
  540. int LSAPI_IsRunning(void)
  541. {
  542. return g_running;
  543. }
  544. int LSAPI_InitRequest( LSAPI_Request * pReq, int fd )
  545. {
  546. if ( !pReq ) {
  547. return -1;
  548. }
  549. memset( pReq, 0, sizeof( LSAPI_Request ) );
  550. if ( allocateIovec( pReq, 16 ) == -1 ) {
  551. return -1;
  552. }
  553. pReq->m_pRespBuf = pReq->m_pRespBufPos = (char *)malloc( LSAPI_RESP_BUF_SIZE );
  554. if ( !pReq->m_pRespBuf ) {
  555. return -1;
  556. }
  557. pReq->m_pRespBufEnd = pReq->m_pRespBuf + LSAPI_RESP_BUF_SIZE;
  558. pReq->m_pIovecCur = pReq->m_pIovecToWrite = pReq->m_pIovec + 1;
  559. pReq->m_respPktHeaderEnd = &pReq->m_respPktHeader[5];
  560. if ( allocateRespHeaderBuf( pReq, LSAPI_INIT_RESP_HEADER_LEN ) == -1 ) {
  561. return -1;
  562. }
  563. if ( isPipe( fd ) ) {
  564. pReq->m_fdListen = -1;
  565. pReq->m_fd = fd;
  566. } else {
  567. pReq->m_fdListen = fd;
  568. pReq->m_fd = -1;
  569. lsapi_set_nblock( fd, 1 );
  570. }
  571. return 0;
  572. }
  573. int LSAPI_Is_Listen( void )
  574. {
  575. return LSAPI_Is_Listen_r( &g_req );
  576. }
  577. int LSAPI_Is_Listen_r( LSAPI_Request * pReq)
  578. {
  579. return pReq->m_fdListen != -1;
  580. }
  581. int LSAPI_Accept_r( LSAPI_Request * pReq )
  582. {
  583. char achPeer[128];
  584. socklen_t len;
  585. int nodelay = 1;
  586. if ( !pReq ) {
  587. return -1;
  588. }
  589. if ( LSAPI_Finish_r( pReq ) == -1 ) {
  590. return -1;
  591. }
  592. while( g_running ) {
  593. if ( pReq->m_fd == -1 ) {
  594. if ( pReq->m_fdListen != -1) {
  595. len = sizeof( achPeer );
  596. pReq->m_fd = accept( pReq->m_fdListen,
  597. (struct sockaddr *)&achPeer, &len );
  598. if ( pReq->m_fd == -1 ) {
  599. if (( errno == EINTR )||( errno == EAGAIN)) {
  600. continue;
  601. } else {
  602. return -1;
  603. }
  604. } else {
  605. lsapi_set_nblock( pReq->m_fd , 0 );
  606. if (((struct sockaddr *)&achPeer)->sa_family == AF_INET ) {
  607. setsockopt(pReq->m_fd, IPPROTO_TCP, TCP_NODELAY,
  608. (char *)&nodelay, sizeof(nodelay));
  609. }
  610. if ( s_accept_notify )
  611. return notify_req_received( pReq->m_fd );
  612. }
  613. } else {
  614. return -1;
  615. }
  616. }
  617. if ( !readReq( pReq ) ) {
  618. break;
  619. }
  620. lsapi_close( pReq->m_fd );
  621. pReq->m_fd = -1;
  622. LSAPI_Reset_r( pReq );
  623. }
  624. return 0;
  625. }
  626. static struct lsapi_packet_header finish = {'L', 'S',
  627. LSAPI_RESP_END, LSAPI_ENDIAN, {LSAPI_PACKET_HEADER_LEN} };
  628. int LSAPI_Finish_r( LSAPI_Request * pReq )
  629. {
  630. /* finish req body */
  631. if ( !pReq ) {
  632. return -1;
  633. }
  634. if (pReq->m_reqState) {
  635. if ( pReq->m_fd != -1 ) {
  636. if ( pReq->m_reqState & LSAPI_ST_RESP_HEADER ) {
  637. LSAPI_FinalizeRespHeaders_r( pReq );
  638. }
  639. if ( pReq->m_pRespBufPos != pReq->m_pRespBuf ) {
  640. Flush_RespBuf_r( pReq );
  641. }
  642. pReq->m_pIovecCur->iov_base = (void *)&finish;
  643. pReq->m_pIovecCur->iov_len = LSAPI_PACKET_HEADER_LEN;
  644. pReq->m_totalLen += LSAPI_PACKET_HEADER_LEN;
  645. ++pReq->m_pIovecCur;
  646. LSAPI_Flush_r( pReq );
  647. }
  648. LSAPI_Reset_r( pReq );
  649. }
  650. return 0;
  651. }
  652. void LSAPI_Reset_r( LSAPI_Request * pReq )
  653. {
  654. pReq->m_pRespBufPos = pReq->m_pRespBuf;
  655. pReq->m_pIovecCur = pReq->m_pIovecToWrite = pReq->m_pIovec + 1;
  656. pReq->m_pRespHeaderBufPos = pReq->m_pRespHeaderBuf;
  657. memset( &pReq->m_pHeaderIndex, 0,
  658. (char *)(pReq->m_respHeaderLen) - (char *)&pReq->m_pHeaderIndex );
  659. }
  660. int LSAPI_Release_r( LSAPI_Request * pReq )
  661. {
  662. if ( pReq->m_pReqBuf ) {
  663. free( pReq->m_pReqBuf );
  664. }
  665. if ( pReq->m_pSpecialEnvList ) {
  666. free( pReq->m_pSpecialEnvList );
  667. }
  668. if ( pReq->m_pEnvList ) {
  669. free( pReq->m_pEnvList );
  670. }
  671. if ( pReq->m_pRespHeaderBuf ) {
  672. free( pReq->m_pRespHeaderBuf );
  673. }
  674. return 0;
  675. }
  676. char * LSAPI_GetHeader_r( LSAPI_Request * pReq, int headerIndex )
  677. {
  678. int off;
  679. if ( !pReq || ((unsigned int)headerIndex > H_TRANSFER_ENCODING) ) {
  680. return NULL;
  681. }
  682. off = pReq->m_pHeaderIndex->m_headerOff[ headerIndex ];
  683. if ( !off ) {
  684. return NULL;
  685. }
  686. if ( *(pReq->m_pHttpHeader + off +
  687. pReq->m_pHeaderIndex->m_headerLen[ headerIndex ]) ) {
  688. *( pReq->m_pHttpHeader + off +
  689. pReq->m_pHeaderIndex->m_headerLen[ headerIndex ]) = 0;
  690. }
  691. return pReq->m_pHttpHeader + off;
  692. }
  693. static int readBodyToReqBuf( LSAPI_Request * pReq )
  694. {
  695. int bodyLeft;
  696. int len = pReq->m_bufRead - pReq->m_bufProcessed;
  697. if ( len > 0 ) {
  698. return len;
  699. }
  700. pReq->m_bufRead = pReq->m_bufProcessed = pReq->m_pHeader->m_pktHeader.m_packetLen.m_iLen;
  701. bodyLeft = pReq->m_pHeader->m_reqBodyLen - pReq->m_reqBodyRead;
  702. len = pReq->m_reqBufSize - pReq->m_bufRead;
  703. if ( len < 0 ) {
  704. return -1;
  705. }
  706. if ( len > bodyLeft ) {
  707. len = bodyLeft;
  708. }
  709. len = lsapi_read( pReq->m_fd, pReq->m_pReqBuf + pReq->m_bufRead, len );
  710. if ( len > 0 ) {
  711. pReq->m_bufRead += len;
  712. }
  713. return len;
  714. }
  715. int LSAPI_ReqBodyGetChar_r( LSAPI_Request * pReq )
  716. {
  717. if (!pReq || (pReq->m_fd ==-1) ) {
  718. return EOF;
  719. }
  720. if ( pReq->m_bufProcessed >= pReq->m_bufRead ) {
  721. if ( readBodyToReqBuf( pReq ) <= 0 ) {
  722. return EOF;
  723. }
  724. }
  725. ++pReq->m_reqBodyRead;
  726. return (unsigned char)*(pReq->m_pReqBuf + pReq->m_bufProcessed++);
  727. }
  728. int LSAPI_ReqBodyGetLine_r( LSAPI_Request * pReq, char * pBuf, int bufLen, int *getLF )
  729. {
  730. int len;
  731. int left;
  732. char * pBufEnd = pBuf + bufLen - 1;
  733. char * pBufCur = pBuf;
  734. char * pCur;
  735. char * p;
  736. if (!pReq || (pReq->m_fd ==-1) ||( !pBuf )||(bufLen < 0 )|| !getLF ) {
  737. return -1;
  738. }
  739. *getLF = 0;
  740. while( (left = pBufEnd - pBufCur ) > 0 ) {
  741. len = pReq->m_bufRead - pReq->m_bufProcessed;
  742. if ( len <= 0 ) {
  743. if ( (len = readBodyToReqBuf( pReq )) <= 0 ) {
  744. *getLF = 1;
  745. break;
  746. }
  747. }
  748. if ( len > left ) {
  749. len = left;
  750. }
  751. pCur = pReq->m_pReqBuf + pReq->m_bufProcessed;
  752. p = memchr( pCur, '\n', len );
  753. if ( p ) {
  754. len = p - pCur + 1;
  755. }
  756. memmove( pBufCur, pCur, len );
  757. pBufCur += len;
  758. pReq->m_bufProcessed += len;
  759. pReq->m_reqBodyRead += len;
  760. if ( p ) {
  761. *getLF = 1;
  762. break;
  763. }
  764. }
  765. *pBufCur = 0;
  766. return pBufCur - pBuf;
  767. }
  768. int LSAPI_ReadReqBody_r( LSAPI_Request * pReq, char * pBuf, int bufLen )
  769. {
  770. int len;
  771. int total;
  772. /* char *pOldBuf = pBuf; */
  773. if (!pReq || (pReq->m_fd ==-1) || ( !pBuf )||(bufLen < 0 )) {
  774. return -1;
  775. }
  776. total = pReq->m_pHeader->m_reqBodyLen - pReq->m_reqBodyRead;
  777. if ( total <= 0 ) {
  778. return 0;
  779. }
  780. if ( total < bufLen ) {
  781. bufLen = total;
  782. }
  783. total = 0;
  784. len = pReq->m_bufRead - pReq->m_bufProcessed;
  785. if ( len > 0 ) {
  786. if ( len > bufLen ) {
  787. len = bufLen;
  788. }
  789. memmove( pBuf, pReq->m_pReqBuf + pReq->m_bufProcessed, len );
  790. pReq->m_bufProcessed += len;
  791. total += len;
  792. pBuf += len;
  793. bufLen -= len;
  794. }
  795. while( bufLen > 0 ) {
  796. len = lsapi_read( pReq->m_fd, pBuf, bufLen );
  797. if ( len > 0 ) {
  798. total += len;
  799. pBuf += len;
  800. bufLen -= len;
  801. } else {
  802. if ( len <= 0 ) {
  803. if ( !total) {
  804. return -1;
  805. }
  806. break;
  807. }
  808. }
  809. }
  810. pReq->m_reqBodyRead += total;
  811. return total;
  812. }
  813. int LSAPI_Write_r( LSAPI_Request * pReq, const char * pBuf, int len )
  814. {
  815. struct lsapi_packet_header * pHeader;
  816. const char * pEnd;
  817. const char * p;
  818. int bufLen;
  819. int toWrite;
  820. int packetLen;
  821. if ( !pReq || !pBuf || (pReq->m_fd == -1) ) {
  822. return -1;
  823. }
  824. if ( len < pReq->m_pRespBufEnd - pReq->m_pRespBufPos ) {
  825. memmove( pReq->m_pRespBufPos, pBuf, len );
  826. pReq->m_pRespBufPos += len;
  827. return len;
  828. }
  829. if ( pReq->m_reqState & LSAPI_ST_RESP_HEADER ) {
  830. LSAPI_FinalizeRespHeaders_r( pReq );
  831. }
  832. pReq->m_reqState |= LSAPI_ST_RESP_BODY;
  833. pHeader = pReq->m_respPktHeader;
  834. p = pBuf;
  835. pEnd = pBuf + len;
  836. bufLen = pReq->m_pRespBufPos - pReq->m_pRespBuf;
  837. while( ( toWrite = pEnd - p ) > 0 ) {
  838. packetLen = toWrite + bufLen;
  839. if ( LSAPI_MAX_DATA_PACKET_LEN < packetLen) {
  840. packetLen = LSAPI_MAX_DATA_PACKET_LEN;
  841. toWrite = packetLen - bufLen;
  842. }
  843. lsapi_buildPacketHeader( pHeader, LSAPI_RESP_STREAM,
  844. packetLen + LSAPI_PACKET_HEADER_LEN );
  845. pReq->m_totalLen += packetLen + LSAPI_PACKET_HEADER_LEN;
  846. pReq->m_pIovecCur->iov_base = (void *)pHeader;
  847. pReq->m_pIovecCur->iov_len = LSAPI_PACKET_HEADER_LEN;
  848. ++pReq->m_pIovecCur;
  849. ++pHeader;
  850. if ( bufLen > 0 ) {
  851. pReq->m_pIovecCur->iov_base = (void *)pReq->m_pRespBuf;
  852. pReq->m_pIovecCur->iov_len = bufLen;
  853. pReq->m_pRespBufPos = pReq->m_pRespBuf;
  854. ++pReq->m_pIovecCur;
  855. bufLen = 0;
  856. }
  857. pReq->m_pIovecCur->iov_base = (void *)p;
  858. pReq->m_pIovecCur->iov_len = toWrite;
  859. ++pReq->m_pIovecCur;
  860. p += toWrite;
  861. if ( pHeader >= pReq->m_respPktHeaderEnd - 1) {
  862. if ( LSAPI_Flush_r( pReq ) == -1 ) {
  863. return -1;
  864. }
  865. pHeader = pReq->m_respPktHeader;
  866. }
  867. }
  868. if ( pHeader != pReq->m_respPktHeader ) {
  869. if ( LSAPI_Flush_r( pReq ) == -1 ) {
  870. return -1;
  871. }
  872. }
  873. return p - pBuf;
  874. }
  875. void Flush_RespBuf_r( LSAPI_Request * pReq )
  876. {
  877. struct lsapi_packet_header * pHeader = pReq->m_respPktHeader;
  878. int bufLen = pReq->m_pRespBufPos - pReq->m_pRespBuf;
  879. pReq->m_reqState |= LSAPI_ST_RESP_BODY;
  880. lsapi_buildPacketHeader( pHeader, LSAPI_RESP_STREAM,
  881. bufLen + LSAPI_PACKET_HEADER_LEN );
  882. pReq->m_totalLen += bufLen + LSAPI_PACKET_HEADER_LEN;
  883. pReq->m_pIovecCur->iov_base = (void *)pHeader;
  884. pReq->m_pIovecCur->iov_len = LSAPI_PACKET_HEADER_LEN;
  885. ++pReq->m_pIovecCur;
  886. ++pHeader;
  887. if ( bufLen > 0 ) {
  888. pReq->m_pIovecCur->iov_base = (void *)pReq->m_pRespBuf;
  889. pReq->m_pIovecCur->iov_len = bufLen;
  890. pReq->m_pRespBufPos = pReq->m_pRespBuf;
  891. ++pReq->m_pIovecCur;
  892. bufLen = 0;
  893. }
  894. }
  895. int LSAPI_Flush_r( LSAPI_Request * pReq )
  896. {
  897. int ret = 0;
  898. int n;
  899. if ( !pReq ) {
  900. return -1;
  901. }
  902. n = pReq->m_pIovecCur - pReq->m_pIovecToWrite;
  903. if (( 0 == n )&&( pReq->m_pRespBufPos == pReq->m_pRespBuf )) {
  904. return 0;
  905. }
  906. if ( pReq->m_fd == -1 ) {
  907. pReq->m_pRespBufPos = pReq->m_pRespBuf;
  908. pReq->m_totalLen = 0;
  909. pReq->m_pIovecCur = pReq->m_pIovecToWrite = pReq->m_pIovec;
  910. return -1;
  911. }
  912. if ( pReq->m_reqState & LSAPI_ST_RESP_HEADER ) {
  913. LSAPI_FinalizeRespHeaders_r( pReq );
  914. }
  915. if ( pReq->m_pRespBufPos != pReq->m_pRespBuf ) {
  916. Flush_RespBuf_r( pReq );
  917. }
  918. n = pReq->m_pIovecCur - pReq->m_pIovecToWrite;
  919. if ( n > 0 ) {
  920. ret = lsapi_writev( pReq->m_fd, &pReq->m_pIovecToWrite,
  921. n, pReq->m_totalLen );
  922. if ( ret < pReq->m_totalLen ) {
  923. lsapi_close( pReq->m_fd );
  924. pReq->m_fd = -1;
  925. ret = -1;
  926. }
  927. pReq->m_totalLen = 0;
  928. pReq->m_pIovecCur = pReq->m_pIovecToWrite = pReq->m_pIovec;
  929. }
  930. return ret;
  931. }
  932. int LSAPI_Write_Stderr_r( LSAPI_Request * pReq, const char * pBuf, int len )
  933. {
  934. struct lsapi_packet_header header;
  935. const char * pEnd;
  936. const char * p;
  937. int packetLen;
  938. int totalLen;
  939. int ret;
  940. struct iovec iov[2];
  941. struct iovec *pIov;
  942. if ( !pReq ) {
  943. return -1;
  944. }
  945. if (( pReq->m_fd == -1 )||(pReq->m_fd == pReq->m_fdListen )) {
  946. return write( 2, pBuf, len );
  947. }
  948. if ( pReq->m_pRespBufPos != pReq->m_pRespBuf ) {
  949. LSAPI_Flush_r( pReq );
  950. }
  951. p = pBuf;
  952. pEnd = pBuf + len;
  953. while( ( packetLen = pEnd - p ) > 0 ) {
  954. if ( LSAPI_MAX_DATA_PACKET_LEN < packetLen) {
  955. packetLen = LSAPI_MAX_DATA_PACKET_LEN;
  956. }
  957. lsapi_buildPacketHeader( &header, LSAPI_STDERR_STREAM,
  958. packetLen + LSAPI_PACKET_HEADER_LEN );
  959. totalLen = packetLen + LSAPI_PACKET_HEADER_LEN;
  960. iov[0].iov_base = (void *)&header;
  961. iov[0].iov_len = LSAPI_PACKET_HEADER_LEN;
  962. iov[1].iov_base = (void *)p;
  963. iov[1].iov_len = packetLen;
  964. p += packetLen;
  965. pIov = iov;
  966. ret = lsapi_writev( pReq->m_fd, &pIov,
  967. 2, totalLen );
  968. if ( ret < totalLen ) {
  969. lsapi_close( pReq->m_fd );
  970. pReq->m_fd = -1;
  971. ret = -1;
  972. }
  973. }
  974. return p - pBuf;
  975. }
  976. static char * GetHeaderVar( LSAPI_Request * pReq, const char * name )
  977. {
  978. int i;
  979. for( i = 0; i < H_TRANSFER_ENCODING; ++i ) {
  980. if ( pReq->m_pHeaderIndex->m_headerOff[i] ) {
  981. if ( strcmp( name, CGI_HEADERS[i] ) == 0 ) {
  982. return pReq->m_pHttpHeader + pReq->m_pHeaderIndex->m_headerOff[i];
  983. }
  984. }
  985. }
  986. if ( pReq->m_pHeader->m_cntUnknownHeaders > 0 ) {
  987. const char *p;
  988. char *pKey;
  989. char *pKeyEnd;
  990. int keyLen;
  991. struct lsapi_header_offset * pCur, *pEnd;
  992. pCur = pReq->m_pUnknownHeader;
  993. pEnd = pCur + pReq->m_pHeader->m_cntUnknownHeaders;
  994. while( pCur < pEnd ) {
  995. pKey = pReq->m_pHttpHeader + pCur->nameOff;
  996. keyLen = pCur->nameLen;
  997. pKeyEnd = pKey + keyLen;
  998. p = &name[5];
  999. while(( pKey < pKeyEnd )&&( *p )) {
  1000. char ch = toupper( *pKey );
  1001. if ((ch != *p )||(( *p == '_' )&&( ch != '-'))) {
  1002. break;
  1003. }
  1004. ++p; ++pKey;
  1005. }
  1006. if (( pKey == pKeyEnd )&& (!*p )) {
  1007. return pReq->m_pHttpHeader + pCur->valueOff;
  1008. }
  1009. ++pCur;
  1010. }
  1011. }
  1012. return NULL;
  1013. }
  1014. char * LSAPI_GetEnv_r( LSAPI_Request * pReq, const char * name )
  1015. {
  1016. struct LSAPI_key_value_pair * pBegin = pReq->m_pEnvList;
  1017. struct LSAPI_key_value_pair * pEnd = pBegin + pReq->m_pHeader->m_cntEnv;
  1018. if ( !pReq || !name ) {
  1019. return NULL;
  1020. }
  1021. if ( strncmp( name, "HTTP_", 5 ) == 0 ) {
  1022. return GetHeaderVar( pReq, name );
  1023. }
  1024. while( pBegin < pEnd ) {
  1025. if ( strcmp( name, pBegin->pKey ) == 0 ) {
  1026. return pBegin->pValue;
  1027. }
  1028. ++pBegin;
  1029. }
  1030. return NULL;
  1031. }
  1032. int LSAPI_ForeachOrgHeader_r( LSAPI_Request * pReq,
  1033. LSAPI_CB_EnvHandler fn, void * arg )
  1034. {
  1035. int i;
  1036. int len = 0;
  1037. char * pValue;
  1038. int ret;
  1039. int count = 0;
  1040. if ( !pReq || !fn ) {
  1041. return -1;
  1042. }
  1043. for( i = 0; i < H_TRANSFER_ENCODING; ++i ) {
  1044. if ( pReq->m_pHeaderIndex->m_headerOff[i] ) {
  1045. len = pReq->m_pHeaderIndex->m_headerLen[i];
  1046. pValue = pReq->m_pHttpHeader + pReq->m_pHeaderIndex->m_headerOff[i];
  1047. *(pValue + len ) = 0;
  1048. ret = (*fn)( HTTP_HEADERS[i], HTTP_HEADER_LEN[i],
  1049. pValue, len, arg );
  1050. ++count;
  1051. if ( ret <= 0 ) {
  1052. return ret;
  1053. }
  1054. }
  1055. }
  1056. if ( pReq->m_pHeader->m_cntUnknownHeaders > 0 ) {
  1057. char *pKey;
  1058. int keyLen;
  1059. struct lsapi_header_offset * pCur, *pEnd;
  1060. pCur = pReq->m_pUnknownHeader;
  1061. pEnd = pCur + pReq->m_pHeader->m_cntUnknownHeaders;
  1062. while( pCur < pEnd ) {
  1063. pKey = pReq->m_pHttpHeader + pCur->nameOff;
  1064. keyLen = pCur->nameLen;
  1065. pValue = pReq->m_pHttpHeader + pCur->valueOff;
  1066. *(pValue + pCur->valueLen ) = 0;
  1067. ret = (*fn)( pKey, keyLen,
  1068. pValue, pCur->valueLen, arg );
  1069. if ( ret <= 0 ) {
  1070. return ret;
  1071. }
  1072. ++pCur;
  1073. }
  1074. }
  1075. return count + pReq->m_pHeader->m_cntUnknownHeaders;
  1076. }
  1077. int LSAPI_ForeachHeader_r( LSAPI_Request * pReq,
  1078. LSAPI_CB_EnvHandler fn, void * arg )
  1079. {
  1080. int i;
  1081. int len = 0;
  1082. char * pValue;
  1083. int ret;
  1084. int count = 0;
  1085. if ( !pReq || !fn ) {
  1086. return -1;
  1087. }
  1088. for( i = 0; i < H_TRANSFER_ENCODING; ++i ) {
  1089. if ( pReq->m_pHeaderIndex->m_headerOff[i] ) {
  1090. len = pReq->m_pHeaderIndex->m_headerLen[i];
  1091. pValue = pReq->m_pHttpHeader + pReq->m_pHeaderIndex->m_headerOff[i];
  1092. *(pValue + len ) = 0;
  1093. ret = (*fn)( CGI_HEADERS[i], CGI_HEADER_LEN[i],
  1094. pValue, len, arg );
  1095. ++count;
  1096. if ( ret <= 0 ) {
  1097. return ret;
  1098. }
  1099. }
  1100. }
  1101. if ( pReq->m_pHeader->m_cntUnknownHeaders > 0 ) {
  1102. char achHeaderName[256];
  1103. char *p;
  1104. char *pKey;
  1105. char *pKeyEnd ;
  1106. int keyLen;
  1107. struct lsapi_header_offset * pCur, *pEnd;
  1108. pCur = pReq->m_pUnknownHeader;
  1109. pEnd = pCur + pReq->m_pHeader->m_cntUnknownHeaders;
  1110. while( pCur < pEnd ) {
  1111. pKey = pReq->m_pHttpHeader + pCur->nameOff;
  1112. keyLen = pCur->nameLen;
  1113. if ( keyLen > 250 ) {
  1114. keyLen = 250;
  1115. }
  1116. pKeyEnd = pKey + keyLen;
  1117. memcpy( achHeaderName, "HTTP_", 5 );
  1118. p = &achHeaderName[5];
  1119. while( pKey < pKeyEnd ) {
  1120. char ch = *pKey++;
  1121. if ( ch == '-' ) {
  1122. *p++ = '_';
  1123. } else {
  1124. *p++ = toupper( ch );
  1125. }
  1126. }
  1127. *p = 0;
  1128. keyLen += 5;
  1129. pValue = pReq->m_pHttpHeader + pCur->valueOff;
  1130. *(pValue + pCur->valueLen ) = 0;
  1131. ret = (*fn)( achHeaderName, keyLen,
  1132. pValue, pCur->valueLen, arg );
  1133. if ( ret <= 0 ) {
  1134. return ret;
  1135. }
  1136. ++pCur;
  1137. }
  1138. }
  1139. return count + pReq->m_pHeader->m_cntUnknownHeaders;
  1140. }
  1141. static int EnvForeach( struct LSAPI_key_value_pair * pEnv,
  1142. int n, LSAPI_CB_EnvHandler fn, void * arg )
  1143. {
  1144. struct LSAPI_key_value_pair * pEnd = pEnv + n;
  1145. int ret;
  1146. if ( !pEnv || !fn ) {
  1147. return -1;
  1148. }
  1149. while( pEnv < pEnd ) {
  1150. ret = (*fn)( pEnv->pKey, pEnv->keyLen,
  1151. pEnv->pValue, pEnv->valLen, arg );
  1152. if ( ret <= 0 ) {
  1153. return ret;
  1154. }
  1155. ++pEnv;
  1156. }
  1157. return n;
  1158. }
  1159. int LSAPI_ForeachEnv_r( LSAPI_Request * pReq,
  1160. LSAPI_CB_EnvHandler fn, void * arg )
  1161. {
  1162. if ( !pReq || !fn ) {
  1163. return -1;
  1164. }
  1165. if ( pReq->m_pHeader->m_cntEnv > 0 ) {
  1166. return EnvForeach( pReq->m_pEnvList, pReq->m_pHeader->m_cntEnv,
  1167. fn, arg );
  1168. }
  1169. return 0;
  1170. }
  1171. int LSAPI_ForeachSpecialEnv_r( LSAPI_Request * pReq,
  1172. LSAPI_CB_EnvHandler fn, void * arg )
  1173. {
  1174. if ( !pReq || !fn ) {
  1175. return -1;
  1176. }
  1177. if ( pReq->m_pHeader->m_cntSpecialEnv > 0 ) {
  1178. return EnvForeach( pReq->m_pSpecialEnvList,
  1179. pReq->m_pHeader->m_cntSpecialEnv,
  1180. fn, arg );
  1181. }
  1182. return 0;
  1183. }
  1184. int LSAPI_FinalizeRespHeaders_r( LSAPI_Request * pReq )
  1185. {
  1186. if ( !pReq || !pReq->m_pIovec ) {
  1187. return -1;
  1188. }
  1189. if ( !( pReq->m_reqState & LSAPI_ST_RESP_HEADER ) ) {
  1190. return 0;
  1191. }
  1192. pReq->m_reqState &= ~LSAPI_ST_RESP_HEADER;
  1193. if ( pReq->m_pRespHeaderBufPos > pReq->m_pRespHeaderBuf ) {
  1194. pReq->m_pIovecCur->iov_base = (void *)pReq->m_pRespHeaderBuf;
  1195. pReq->m_pIovecCur->iov_len = pReq->m_pRespHeaderBufPos - pReq->m_pRespHeaderBuf;
  1196. pReq->m_totalLen += pReq->m_pIovecCur->iov_len;
  1197. ++pReq->m_pIovecCur;
  1198. }
  1199. pReq->m_pIovec->iov_len = sizeof( struct lsapi_resp_header)
  1200. + pReq->m_respHeader.m_respInfo.m_cntHeaders * sizeof( short );
  1201. pReq->m_totalLen += pReq->m_pIovec->iov_len;
  1202. lsapi_buildPacketHeader( &pReq->m_respHeader.m_pktHeader,
  1203. LSAPI_RESP_HEADER, pReq->m_totalLen );
  1204. pReq->m_pIovec->iov_base = (void *)&pReq->m_respHeader;
  1205. pReq->m_pIovecToWrite = pReq->m_pIovec;
  1206. return 0;
  1207. }
  1208. int LSAPI_AppendRespHeader_r( LSAPI_Request * pReq, char * pBuf, int len )
  1209. {
  1210. if ( !pReq || !pBuf || len <= 0 || len > LSAPI_RESP_HTTP_HEADER_MAX ) {
  1211. return -1;
  1212. }
  1213. if ( pReq->m_reqState & LSAPI_ST_RESP_BODY ) {
  1214. return -1;
  1215. }
  1216. if ( pReq->m_respHeader.m_respInfo.m_cntHeaders >= LSAPI_MAX_RESP_HEADERS ) {
  1217. return -1;
  1218. }
  1219. if ( pReq->m_pRespHeaderBufPos + len + 1 > pReq->m_pRespHeaderBufEnd ) {
  1220. int newlen = pReq->m_pRespHeaderBufPos + len + 4096 - pReq->m_pRespHeaderBuf;
  1221. newlen -= newlen % 4096;
  1222. if ( allocateRespHeaderBuf( pReq, newlen ) == -1 ) {
  1223. return -1;
  1224. }
  1225. }
  1226. memmove( pReq->m_pRespHeaderBufPos, pBuf, len );
  1227. pReq->m_pRespHeaderBufPos += len;
  1228. *pReq->m_pRespHeaderBufPos++ = 0;
  1229. ++len; /* add one byte padding for \0 */
  1230. pReq->m_respHeaderLen[pReq->m_respHeader.m_respInfo.m_cntHeaders] = len;
  1231. ++pReq->m_respHeader.m_respInfo.m_cntHeaders;
  1232. return 0;
  1233. }
  1234. int LSAPI_CreateListenSock2( const struct sockaddr * pServerAddr, int backlog )
  1235. {
  1236. int ret;
  1237. int fd;
  1238. int flag = 1;
  1239. int addr_len;
  1240. switch( pServerAddr->sa_family ) {
  1241. case AF_INET:
  1242. addr_len = 16;
  1243. break;
  1244. case AF_INET6:
  1245. addr_len = sizeof( struct sockaddr_in6 );
  1246. break;
  1247. case AF_UNIX:
  1248. addr_len = sizeof( struct sockaddr_un );
  1249. unlink( ((struct sockaddr_un *)pServerAddr)->sun_path );
  1250. break;
  1251. default:
  1252. return -1;
  1253. }
  1254. fd = socket( pServerAddr->sa_family, SOCK_STREAM, 0 );
  1255. if ( fd == -1 ) {
  1256. return -1;
  1257. }
  1258. fcntl( fd, F_SETFD, FD_CLOEXEC );
  1259. if(setsockopt( fd, SOL_SOCKET, SO_REUSEADDR,
  1260. (char *)( &flag ), sizeof(flag)) == 0) {
  1261. ret = bind( fd, pServerAddr, addr_len );
  1262. if ( !ret ) {
  1263. ret = listen( fd, backlog );
  1264. if ( !ret ) {
  1265. return fd;
  1266. }
  1267. }
  1268. }
  1269. ret = errno;
  1270. close(fd);
  1271. errno = ret;
  1272. return -1;
  1273. }
  1274. int LSAPI_ParseSockAddr( const char * pBind, struct sockaddr * pAddr )
  1275. {
  1276. char achAddr[256];
  1277. char * p = achAddr;
  1278. char * pEnd;
  1279. struct addrinfo *res, hints;
  1280. int doAddrInfo = 0;
  1281. int port;
  1282. if ( !pBind ) {
  1283. return -1;
  1284. }
  1285. while( isspace( *p ) ) {
  1286. ++pBind;
  1287. }
  1288. strncpy( achAddr, pBind, 256 );
  1289. switch( *p ) {
  1290. case '/':
  1291. pAddr->sa_family = AF_UNIX;
  1292. strncpy( ((struct sockaddr_un *)pAddr)->sun_path, p,
  1293. sizeof(((struct sockaddr_un *)pAddr)->sun_path) );
  1294. return 0;
  1295. case '[':
  1296. pAddr->sa_family = AF_INET6;
  1297. ++p;
  1298. pEnd = strchr( p, ']' );
  1299. if ( !pEnd )
  1300. return -1;
  1301. *pEnd++ = 0;
  1302. if ( *p == '*' ) {
  1303. strcpy( achAddr, "::" );
  1304. p = achAddr;
  1305. }
  1306. doAddrInfo = 1;
  1307. break;
  1308. default:
  1309. pAddr->sa_family = AF_INET;
  1310. pEnd = strchr( p, ':' );
  1311. if ( !pEnd ) {
  1312. return -1;
  1313. }
  1314. *pEnd++ = 0;
  1315. doAddrInfo = 0;
  1316. if ( *p == '*' ) {
  1317. ((struct sockaddr_in *)pAddr)->sin_addr.s_addr = htonl(INADDR_ANY);
  1318. } else {
  1319. if (!strcasecmp( p, "localhost" ) ) {
  1320. ((struct sockaddr_in *)pAddr)->sin_addr.s_addr = htonl( INADDR_LOOPBACK );
  1321. } else {
  1322. ((struct sockaddr_in *)pAddr)->sin_addr.s_addr = inet_addr( p );
  1323. if ( ((struct sockaddr_in *)pAddr)->sin_addr.s_addr == INADDR_BROADCAST) {
  1324. doAddrInfo = 1;
  1325. }
  1326. }
  1327. }
  1328. break;
  1329. }
  1330. if ( *pEnd == ':' ) {
  1331. ++pEnd;
  1332. }
  1333. port = atoi( pEnd );
  1334. if (( port <= 0 )||( port > 655535 )) {
  1335. return -1;
  1336. }
  1337. if ( doAddrInfo ) {
  1338. memset(&hints, 0, sizeof(hints));
  1339. hints.ai_family = pAddr->sa_family;
  1340. hints.ai_socktype = SOCK_STREAM;
  1341. hints.ai_protocol = IPPROTO_TCP;
  1342. if ( getaddrinfo(p, NULL, &hints, &res) ) {
  1343. return -1;
  1344. }
  1345. memcpy(pAddr, res->ai_addr, res->ai_addrlen);
  1346. freeaddrinfo(res);
  1347. }
  1348. if ( pAddr->sa_family == AF_INET ) {
  1349. ((struct sockaddr_in *)pAddr)->sin_port = htons( port );
  1350. } else {
  1351. ((struct sockaddr_in6 *)pAddr)->sin6_port = htons( port );
  1352. }
  1353. return 0;
  1354. }
  1355. int LSAPI_CreateListenSock( const char * pBind, int backlog )
  1356. {
  1357. char serverAddr[128];
  1358. int ret;
  1359. int fd = -1;
  1360. ret = LSAPI_ParseSockAddr( pBind, (struct sockaddr *)serverAddr );
  1361. if ( !ret ) {
  1362. fd = LSAPI_CreateListenSock2( (struct sockaddr *)serverAddr, backlog );
  1363. }
  1364. return fd;
  1365. }
  1366. static fn_select_t g_fnSelect = select;
  1367. typedef struct _lsapi_child_status
  1368. {
  1369. int m_pid;
  1370. volatile short m_iKillSent;
  1371. volatile short m_inProcess;
  1372. volatile long m_tmWaitBegin;
  1373. volatile long m_tmReqBegin;
  1374. volatile long m_tmLastCheckPoint;
  1375. }
  1376. lsapi_child_status;
  1377. static lsapi_child_status * s_pChildStatus = NULL;
  1378. typedef struct _lsapi_prefork_server
  1379. {
  1380. int m_fd;
  1381. int m_iMaxChildren;
  1382. int m_iExtraChildren;
  1383. int m_iCurChildren;
  1384. int m_iMaxIdleChildren;
  1385. int m_iServerMaxIdle;
  1386. int m_iChildrenMaxIdleTime;
  1387. int m_iMaxReqProcessTime;
  1388. int m_iAvoidFork;
  1389. lsapi_child_status * m_pChildrenStatus;
  1390. }lsapi_prefork_server;
  1391. static lsapi_prefork_server * g_prefork_server = NULL;
  1392. int LSAPI_Init_Prefork_Server( int max_children, fn_select_t fp, int avoidFork )
  1393. {
  1394. if ( g_prefork_server ) {
  1395. return 0;
  1396. }
  1397. if ( max_children <= 1 ) {
  1398. return -1;
  1399. }
  1400. if ( max_children >= 10000) {
  1401. max_children = 10000;
  1402. }
  1403. g_prefork_server = (lsapi_prefork_server *)malloc( sizeof( lsapi_prefork_server ) );
  1404. if ( !g_prefork_server ) {
  1405. return -1;
  1406. }
  1407. memset( g_prefork_server, 0, sizeof( lsapi_prefork_server ) );
  1408. if ( fp != NULL ) {
  1409. g_fnSelect = fp;
  1410. }
  1411. s_ppid = getppid();
  1412. g_prefork_server->m_iAvoidFork = avoidFork;
  1413. g_prefork_server->m_iMaxChildren = max_children;
  1414. g_prefork_server->m_iExtraChildren = ( avoidFork ) ? 0 : (max_children / 3) ;
  1415. g_prefork_server->m_iMaxIdleChildren = ( avoidFork ) ? (max_children + 1) : (max_children / 3);
  1416. g_prefork_server->m_iChildrenMaxIdleTime = 300;
  1417. g_prefork_server->m_iMaxReqProcessTime = 300;
  1418. return 0;
  1419. }
  1420. void LSAPI_Set_Server_fd( int fd )
  1421. {
  1422. if( g_prefork_server ) {
  1423. g_prefork_server->m_fd = fd;
  1424. }
  1425. }
  1426. static int lsapi_accept( int fdListen )
  1427. {
  1428. int fd;
  1429. int nodelay = 1;
  1430. socklen_t len;
  1431. char achPeer[128];
  1432. len = sizeof( achPeer );
  1433. fd = accept( fdListen, (struct sockaddr *)&achPeer, &len );
  1434. if ( fd != -1 ) {
  1435. if (((struct sockaddr *)&achPeer)->sa_family == AF_INET ) {
  1436. setsockopt( fd, IPPROTO_TCP, TCP_NODELAY,
  1437. (char *)&nodelay, sizeof(nodelay));
  1438. }
  1439. if ( s_accept_notify )
  1440. notify_req_received( fd );
  1441. }
  1442. return fd;
  1443. }
  1444. static int s_req_processed = 0;
  1445. static int s_max_reqs = 10000;
  1446. static int s_max_idle_secs = 300;
  1447. static int s_stop;
  1448. static void lsapi_cleanup(int signal)
  1449. {
  1450. s_stop = signal;
  1451. }
  1452. static lsapi_child_status * find_child_status( int pid )
  1453. {
  1454. lsapi_child_status * pStatus = g_prefork_server->m_pChildrenStatus;
  1455. lsapi_child_status * pEnd = g_prefork_server->m_pChildrenStatus + g_prefork_server->m_iMaxChildren * 2;
  1456. while( pStatus < pEnd ) {
  1457. if ( pStatus->m_pid == pid ) {
  1458. return pStatus;
  1459. }
  1460. ++pStatus;
  1461. }
  1462. return NULL;
  1463. }
  1464. static void lsapi_sigchild( int signal )
  1465. {
  1466. int status, pid;
  1467. lsapi_child_status * child_status;
  1468. while( 1 ) {
  1469. pid = waitpid( -1, &status, WNOHANG|WUNTRACED );
  1470. if ( pid <= 0 ) {
  1471. break;
  1472. }
  1473. child_status = find_child_status( pid );
  1474. if ( child_status ) {
  1475. child_status->m_pid = 0;
  1476. }
  1477. --g_prefork_server->m_iCurChildren;
  1478. }
  1479. }
  1480. static int lsapi_init_children_status()
  1481. {
  1482. int size = 4096;
  1483. char * pBuf;
  1484. size = g_prefork_server->m_iMaxChildren * sizeof( lsapi_child_status ) * 2;
  1485. size = (size + 4095 ) / 4096 * 4096;
  1486. pBuf =( char*) mmap( NULL, size, PROT_READ | PROT_WRITE,
  1487. MAP_ANON | MAP_SHARED, -1, 0 );
  1488. if ( pBuf == MAP_FAILED ) {
  1489. perror( "Anonymous mmap() failed" );
  1490. return -1;
  1491. }
  1492. memset( pBuf, 0, size );
  1493. g_prefork_server->m_pChildrenStatus = (lsapi_child_status *)pBuf;
  1494. return 0;
  1495. }
  1496. static void lsapi_check_child_status( long tmCur )
  1497. {
  1498. int idle = 0;
  1499. int tobekilled;
  1500. int dying = 0;
  1501. lsapi_child_status * pStatus = g_prefork_server->m_pChildrenStatus;
  1502. lsapi_child_status * pEnd = g_prefork_server->m_pChildrenStatus + g_prefork_server->m_iMaxChildren * 2;
  1503. while( pStatus < pEnd ) {
  1504. tobekilled = pStatus->m_iKillSent;
  1505. if ( pStatus->m_pid != 0 ) {
  1506. if ( !tobekilled ) {
  1507. if ( !pStatus->m_inProcess ) {
  1508. if (( g_prefork_server->m_iCurChildren - dying > g_prefork_server->m_iMaxChildren)||
  1509. ( idle >= g_prefork_server->m_iMaxIdleChildren )) {
  1510. tobekilled = 1;
  1511. } else {
  1512. if (( s_max_idle_secs> 0)&&(tmCur - pStatus->m_tmWaitBegin > s_max_idle_secs + 5 )) {
  1513. tobekilled = 1;
  1514. }
  1515. }
  1516. if ( !tobekilled ) {
  1517. ++idle;
  1518. }
  1519. } else {
  1520. if ( tmCur - pStatus->m_tmReqBegin >
  1521. g_prefork_server->m_iMaxReqProcessTime ) {
  1522. tobekilled = 1;
  1523. }
  1524. }
  1525. } else {
  1526. if ( pStatus->m_inProcess ) {
  1527. tobekilled = pStatus->m_iKillSent = 0;
  1528. }
  1529. }
  1530. if ( tobekilled ) {
  1531. tobekilled = 0;
  1532. if ( pStatus->m_iKillSent > 5 ) {
  1533. tobekilled = SIGKILL;
  1534. } else {
  1535. if ( pStatus->m_iKillSent == 3 ) {
  1536. tobekilled = SIGTERM;
  1537. } else {
  1538. if ( pStatus->m_iKillSent == 1 ) {
  1539. tobekilled = SIGUSR1;
  1540. }
  1541. }
  1542. }
  1543. if ( tobekilled ) {
  1544. kill( pStatus->m_pid, tobekilled );
  1545. }
  1546. ++pStatus->m_iKillSent;
  1547. ++dying;
  1548. }
  1549. } else {
  1550. ++dying;
  1551. }
  1552. ++pStatus;
  1553. }
  1554. }
  1555. static int lsapi_all_children_must_die()
  1556. {
  1557. int maxWait;
  1558. int sec =0;
  1559. g_prefork_server->m_iMaxReqProcessTime = 10;
  1560. g_prefork_server->m_iMaxIdleChildren = -1;
  1561. maxWait = 15;
  1562. while( g_prefork_server->m_iCurChildren && (sec < maxWait) ) {
  1563. lsapi_check_child_status(time(NULL));
  1564. sleep( 1 );
  1565. sec++;
  1566. }
  1567. if ( g_prefork_server->m_iCurChildren != 0 ) {
  1568. kill( -getpgrp(), SIGKILL );
  1569. }
  1570. return 0;
  1571. }
  1572. static int lsapi_prefork_server_accept( lsapi_prefork_server * pServer, LSAPI_Request * pReq )
  1573. {
  1574. struct sigaction act, old_term, old_quit, old_int,
  1575. old_usr1, old_child;
  1576. lsapi_child_status * child_status;

Large files files are truncated, but you can click here to view the full file