1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2017 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
19 /*
20 Copyright (c) 2002-2015, Lite Speed Technologies Inc.
21 All rights reserved.
22
23 Redistribution and use in source and binary forms, with or without
24 modification, are permitted provided that the following conditions are
25 met:
26
27 * Redistributions of source code must retain the above copyright
28 notice, this list of conditions and the following disclaimer.
29 * Redistributions in binary form must reproduce the above
30 copyright notice, this list of conditions and the following
31 disclaimer in the documentation and/or other materials provided
32 with the distribution.
33 * Neither the name of the Lite Speed Technologies Inc nor the
34 names of its contributors may be used to endorse or promote
35 products derived from this software without specific prior
36 written permission.
37
38 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
41 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
42 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
45 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
46 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
47 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
48 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49 */
50
51
52 #ifndef _LSAPILIB_H_
53 #define _LSAPILIB_H_
54
55 #if defined (c_plusplus) || defined (__cplusplus)
56 extern "C" {
57 #endif
58
59 #include <stddef.h>
60 #include <lsapidef.h>
61
62 #include <sys/time.h>
63 #include <sys/types.h>
64
65 struct LSAPI_key_value_pair
66 {
67 char * pKey;
68 char * pValue;
69 int keyLen;
70 int valLen;
71 };
72
73
74 #define LSAPI_MAX_RESP_HEADERS 100
75
76 typedef struct lsapi_request
77 {
78 int m_fdListen;
79 int m_fd;
80
81 long m_lLastActive;
82 long m_lReqBegin;
83
84 char * m_pReqBuf;
85 int m_reqBufSize;
86
87 char * m_pRespBuf;
88 char * m_pRespBufEnd;
89 char * m_pRespBufPos;
90
91 char * m_pRespHeaderBuf;
92 char * m_pRespHeaderBufEnd;
93 char * m_pRespHeaderBufPos;
94
95
96 struct iovec * m_pIovec;
97 struct iovec * m_pIovecEnd;
98 struct iovec * m_pIovecCur;
99 struct iovec * m_pIovecToWrite;
100
101 struct lsapi_packet_header * m_respPktHeaderEnd;
102
103 struct lsapi_req_header * m_pHeader;
104 struct LSAPI_key_value_pair * m_pEnvList;
105 struct LSAPI_key_value_pair * m_pSpecialEnvList;
106 int m_envListSize;
107 int m_specialEnvListSize;
108
109 struct lsapi_http_header_index * m_pHeaderIndex;
110 struct lsapi_header_offset * m_pUnknownHeader;
111
112 char * m_pScriptFile;
113 char * m_pScriptName;
114 char * m_pQueryString;
115 char * m_pHttpHeader;
116 char * m_pRequestMethod;
117 int m_totalLen;
118 int m_reqState;
119 off_t m_reqBodyLen;
120 off_t m_reqBodyRead;
121 int m_bufProcessed;
122 int m_bufRead;
123
124 struct lsapi_packet_header m_respPktHeader[5];
125
126 struct lsapi_resp_header m_respHeader;
127 short m_respHeaderLen[LSAPI_MAX_RESP_HEADERS];
128 void * m_pAppData;
129
130 }LSAPI_Request;
131
132 extern LSAPI_Request g_req;
133
134
135 /* return: >0 continue, ==0 stop, -1 failed */
136 typedef int (*LSAPI_CB_EnvHandler )( const char * pKey, int keyLen,
137 const char * pValue, int valLen, void * arg );
138
139
140 int LSAPI_Init(void);
141
142 void LSAPI_Stop(void);
143
144 int LSAPI_Is_Listen_r( LSAPI_Request * pReq);
145
146 int LSAPI_InitRequest( LSAPI_Request * pReq, int fd );
147
148 int LSAPI_Accept_r( LSAPI_Request * pReq );
149
150 void LSAPI_Reset_r( LSAPI_Request * pReq );
151
152 int LSAPI_Finish_r( LSAPI_Request * pReq );
153
154 int LSAPI_Release_r( LSAPI_Request * pReq );
155
156 char * LSAPI_GetHeader_r( LSAPI_Request * pReq, int headerIndex );
157
158 int LSAPI_ForeachHeader_r( LSAPI_Request * pReq,
159 LSAPI_CB_EnvHandler fn, void * arg );
160
161 int LSAPI_ForeachOrgHeader_r( LSAPI_Request * pReq,
162 LSAPI_CB_EnvHandler fn, void * arg );
163
164 int LSAPI_ForeachEnv_r( LSAPI_Request * pReq,
165 LSAPI_CB_EnvHandler fn, void * arg );
166
167 int LSAPI_ForeachSpecialEnv_r( LSAPI_Request * pReq,
168 LSAPI_CB_EnvHandler fn, void * arg );
169
170 char * LSAPI_GetEnv_r( LSAPI_Request * pReq, const char * name );
171
172
173 ssize_t LSAPI_ReadReqBody_r( LSAPI_Request * pReq, char * pBuf, size_t len );
174
175 int LSAPI_ReqBodyGetChar_r( LSAPI_Request * pReq );
176
177 int LSAPI_ReqBodyGetLine_r( LSAPI_Request * pReq, char * pBuf, size_t bufLen, int *getLF );
178
179
180 int LSAPI_FinalizeRespHeaders_r( LSAPI_Request * pReq );
181
182 ssize_t LSAPI_Write_r( LSAPI_Request * pReq, const char * pBuf, size_t len );
183
184 ssize_t LSAPI_sendfile_r( LSAPI_Request * pReq, int fdIn, off_t* off, size_t size );
185
186 ssize_t LSAPI_Write_Stderr_r( LSAPI_Request * pReq, const char * pBuf, size_t len );
187
188 int LSAPI_Flush_r( LSAPI_Request * pReq );
189
190 int LSAPI_AppendRespHeader_r( LSAPI_Request * pReq, const char * pBuf, int len );
191
192 int LSAPI_AppendRespHeader2_r( LSAPI_Request * pReq, const char * pHeaderName,
193 const char * pHeaderValue );
194
195 int LSAPI_ErrResponse_r( LSAPI_Request * pReq, int code, const char ** pRespHeaders,
196 const char * pBody, int bodyLen );
197
LSAPI_SetRespStatus_r(LSAPI_Request * pReq,int code)198 static inline int LSAPI_SetRespStatus_r( LSAPI_Request * pReq, int code )
199 {
200 if ( !pReq )
201 return -1;
202 pReq->m_respHeader.m_respInfo.m_status = code;
203 return 0;
204 }
205
LSAPI_SetAppData_r(LSAPI_Request * pReq,void * data)206 static inline int LSAPI_SetAppData_r( LSAPI_Request * pReq, void * data )
207 {
208 if ( !pReq )
209 return -1;
210 pReq->m_pAppData = data;
211 return 0;
212 }
213
LSAPI_GetAppData_r(LSAPI_Request * pReq)214 static inline void * LSAPI_GetAppData_r( LSAPI_Request * pReq )
215 {
216 if ( !pReq )
217 return NULL;
218 return pReq->m_pAppData;
219 }
220
LSAPI_GetQueryString_r(LSAPI_Request * pReq)221 static inline char * LSAPI_GetQueryString_r( LSAPI_Request * pReq )
222 {
223 if ( pReq )
224 return pReq->m_pQueryString;
225 return NULL;
226 }
227
228
LSAPI_GetScriptFileName_r(LSAPI_Request * pReq)229 static inline char * LSAPI_GetScriptFileName_r( LSAPI_Request * pReq )
230 {
231 if ( pReq )
232 return pReq->m_pScriptFile;
233 return NULL;
234 }
235
236
LSAPI_GetScriptName_r(LSAPI_Request * pReq)237 static inline char * LSAPI_GetScriptName_r( LSAPI_Request * pReq )
238 {
239 if ( pReq )
240 return pReq->m_pScriptName;
241 return NULL;
242 }
243
244
LSAPI_GetRequestMethod_r(LSAPI_Request * pReq)245 static inline char * LSAPI_GetRequestMethod_r( LSAPI_Request * pReq)
246 {
247 if ( pReq )
248 return pReq->m_pRequestMethod;
249 return NULL;
250 }
251
252
253
LSAPI_GetReqBodyLen_r(LSAPI_Request * pReq)254 static inline off_t LSAPI_GetReqBodyLen_r( LSAPI_Request * pReq )
255 {
256 if ( pReq )
257 return pReq->m_reqBodyLen;
258 return -1;
259 }
260
LSAPI_GetReqBodyRemain_r(LSAPI_Request * pReq)261 static inline off_t LSAPI_GetReqBodyRemain_r( LSAPI_Request * pReq )
262 {
263 if ( pReq )
264 return pReq->m_reqBodyLen - pReq->m_reqBodyRead;
265 return -1;
266 }
267
268
269
270 int LSAPI_Is_Listen(void);
271
LSAPI_Accept(void)272 static inline int LSAPI_Accept( void )
273 { return LSAPI_Accept_r( &g_req ); }
274
LSAPI_Finish(void)275 static inline int LSAPI_Finish(void)
276 { return LSAPI_Finish_r( &g_req ); }
277
LSAPI_GetHeader(int headerIndex)278 static inline char * LSAPI_GetHeader( int headerIndex )
279 { return LSAPI_GetHeader_r( &g_req, headerIndex ); }
280
LSAPI_ForeachHeader(LSAPI_CB_EnvHandler fn,void * arg)281 static inline int LSAPI_ForeachHeader( LSAPI_CB_EnvHandler fn, void * arg )
282 { return LSAPI_ForeachHeader_r( &g_req, fn, arg ); }
283
LSAPI_ForeachOrgHeader(LSAPI_CB_EnvHandler fn,void * arg)284 static inline int LSAPI_ForeachOrgHeader(
285 LSAPI_CB_EnvHandler fn, void * arg )
286 { return LSAPI_ForeachOrgHeader_r( &g_req, fn, arg ); }
287
LSAPI_ForeachEnv(LSAPI_CB_EnvHandler fn,void * arg)288 static inline int LSAPI_ForeachEnv( LSAPI_CB_EnvHandler fn, void * arg )
289 { return LSAPI_ForeachEnv_r( &g_req, fn, arg ); }
290
LSAPI_ForeachSpecialEnv(LSAPI_CB_EnvHandler fn,void * arg)291 static inline int LSAPI_ForeachSpecialEnv( LSAPI_CB_EnvHandler fn, void * arg )
292 { return LSAPI_ForeachSpecialEnv_r( &g_req, fn, arg ); }
293
LSAPI_GetEnv(const char * name)294 static inline char * LSAPI_GetEnv( const char * name )
295 { return LSAPI_GetEnv_r( &g_req, name ); }
296
LSAPI_GetQueryString()297 static inline char * LSAPI_GetQueryString()
298 { return LSAPI_GetQueryString_r( &g_req ); }
299
LSAPI_GetScriptFileName()300 static inline char * LSAPI_GetScriptFileName()
301 { return LSAPI_GetScriptFileName_r( &g_req ); }
302
LSAPI_GetScriptName()303 static inline char * LSAPI_GetScriptName()
304 { return LSAPI_GetScriptName_r( &g_req ); }
305
LSAPI_GetRequestMethod()306 static inline char * LSAPI_GetRequestMethod()
307 { return LSAPI_GetRequestMethod_r( &g_req ); }
308
LSAPI_GetReqBodyLen()309 static inline off_t LSAPI_GetReqBodyLen()
310 { return LSAPI_GetReqBodyLen_r( &g_req ); }
311
LSAPI_GetReqBodyRemain()312 static inline off_t LSAPI_GetReqBodyRemain()
313 { return LSAPI_GetReqBodyRemain_r( &g_req ); }
314
LSAPI_ReadReqBody(char * pBuf,size_t len)315 static inline ssize_t LSAPI_ReadReqBody( char * pBuf, size_t len )
316 { return LSAPI_ReadReqBody_r( &g_req, pBuf, len ); }
317
LSAPI_ReqBodyGetChar()318 static inline int LSAPI_ReqBodyGetChar()
319 { return LSAPI_ReqBodyGetChar_r( &g_req ); }
320
LSAPI_ReqBodyGetLine(char * pBuf,int len,int * getLF)321 static inline int LSAPI_ReqBodyGetLine( char * pBuf, int len, int *getLF )
322 { return LSAPI_ReqBodyGetLine_r( &g_req, pBuf, len, getLF ); }
323
324
325
LSAPI_FinalizeRespHeaders(void)326 static inline int LSAPI_FinalizeRespHeaders(void)
327 { return LSAPI_FinalizeRespHeaders_r( &g_req ); }
328
LSAPI_Write(const char * pBuf,ssize_t len)329 static inline ssize_t LSAPI_Write( const char * pBuf, ssize_t len )
330 { return LSAPI_Write_r( &g_req, pBuf, len ); }
331
LSAPI_sendfile(int fdIn,off_t * off,size_t size)332 static inline ssize_t LSAPI_sendfile( int fdIn, off_t* off, size_t size )
333 {
334 return LSAPI_sendfile_r(&g_req, fdIn, off, size );
335 }
336
LSAPI_Write_Stderr(const char * pBuf,ssize_t len)337 static inline ssize_t LSAPI_Write_Stderr( const char * pBuf, ssize_t len )
338 { return LSAPI_Write_Stderr_r( &g_req, pBuf, len ); }
339
LSAPI_Flush()340 static inline int LSAPI_Flush()
341 { return LSAPI_Flush_r( &g_req ); }
342
LSAPI_AppendRespHeader(char * pBuf,int len)343 static inline int LSAPI_AppendRespHeader( char * pBuf, int len )
344 { return LSAPI_AppendRespHeader_r( &g_req, pBuf, len ); }
345
LSAPI_SetRespStatus(int code)346 static inline int LSAPI_SetRespStatus( int code )
347 { return LSAPI_SetRespStatus_r( &g_req, code ); }
348
LSAPI_ErrResponse(int code,const char ** pRespHeaders,const char * pBody,int bodyLen)349 static inline int LSAPI_ErrResponse( int code, const char ** pRespHeaders, const char * pBody, int bodyLen )
350 { return LSAPI_ErrResponse_r( &g_req, code, pRespHeaders, pBody, bodyLen ); }
351
352 int LSAPI_IsRunning(void);
353
354 int LSAPI_CreateListenSock( const char * pBind, int backlog );
355
356 typedef int (*fn_select_t)( int, fd_set *, fd_set *, fd_set *, struct timeval * );
357
358 int LSAPI_Init_Prefork_Server( int max_children, fn_select_t fp, int avoidFork );
359
360 void LSAPI_Set_Server_fd( int fd );
361
362 int LSAPI_Prefork_Accept_r( LSAPI_Request * pReq );
363
364 void LSAPI_No_Check_ppid(void);
365
366 void LSAPI_Set_Max_Reqs( int reqs );
367
368 void LSAPI_Set_Max_Idle( int secs );
369
370 void LSAPI_Set_Max_Children( int maxChildren );
371
372 void LSAPI_Set_Max_Idle_Children( int maxIdleChld );
373
374 void LSAPI_Set_Server_Max_Idle_Secs( int serverMaxIdle );
375
376 void LSAPI_Set_Max_Process_Time( int secs );
377
378 int LSAPI_Init_Env_Parameters( fn_select_t fp );
379
380 void LSAPI_Set_Slow_Req_Msecs( int msecs );
381
382 int LSAPI_Get_Slow_Req_Msecs( );
383
384 int LSAPI_is_suEXEC_Daemon();
385
386 #if defined (c_plusplus) || defined (__cplusplus)
387 }
388 #endif
389
390
391 #endif
392
393
394
395
396
397
398
399