1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2018 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-2018, 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 "lsapidef.h"
60
61 #include <stddef.h>
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 struct lsapi_child_status;
74 #define LSAPI_MAX_RESP_HEADERS 1000
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 struct lsapi_child_status * child_status;
95
96
97 struct iovec * m_pIovec;
98 struct iovec * m_pIovecEnd;
99 struct iovec * m_pIovecCur;
100 struct iovec * m_pIovecToWrite;
101
102 struct lsapi_packet_header * m_respPktHeaderEnd;
103
104 struct lsapi_req_header * m_pHeader;
105 struct LSAPI_key_value_pair * m_pEnvList;
106 struct LSAPI_key_value_pair * m_pSpecialEnvList;
107 int m_envListSize;
108 int m_specialEnvListSize;
109
110 struct lsapi_http_header_index * m_pHeaderIndex;
111 struct lsapi_header_offset * m_pUnknownHeader;
112
113 char * m_pScriptFile;
114 char * m_pScriptName;
115 char * m_pQueryString;
116 char * m_pHttpHeader;
117 char * m_pRequestMethod;
118 int m_totalLen;
119 int m_reqState;
120 off_t m_reqBodyLen;
121 off_t m_reqBodyRead;
122 int m_bufProcessed;
123 int m_bufRead;
124
125 struct lsapi_packet_header m_respPktHeader[5];
126
127 struct lsapi_resp_header m_respHeader;
128 short m_respHeaderLen[LSAPI_MAX_RESP_HEADERS];
129 void * m_pAppData;
130
131 }LSAPI_Request;
132
133 extern LSAPI_Request g_req;
134
135
136 /* return: >0 continue, ==0 stop, -1 failed */
137 typedef int (*LSAPI_CB_EnvHandler )( const char * pKey, int keyLen,
138 const char * pValue, int valLen, void * arg );
139
140
141 int LSAPI_Init(void);
142
143 void LSAPI_Stop(void);
144
145 int LSAPI_Is_Listen_r( LSAPI_Request * pReq);
146
147 int LSAPI_InitRequest( LSAPI_Request * pReq, int fd );
148
149 int LSAPI_Accept_r( LSAPI_Request * pReq );
150
151 void LSAPI_Reset_r( LSAPI_Request * pReq );
152
153 int LSAPI_Finish_r( LSAPI_Request * pReq );
154
155 int LSAPI_Release_r( LSAPI_Request * pReq );
156
157 char * LSAPI_GetHeader_r( LSAPI_Request * pReq, int headerIndex );
158
159 int LSAPI_ForeachHeader_r( LSAPI_Request * pReq,
160 LSAPI_CB_EnvHandler fn, void * arg );
161
162 int LSAPI_ForeachOrgHeader_r( LSAPI_Request * pReq,
163 LSAPI_CB_EnvHandler fn, void * arg );
164
165 int LSAPI_ForeachEnv_r( LSAPI_Request * pReq,
166 LSAPI_CB_EnvHandler fn, void * arg );
167
168 int LSAPI_ForeachSpecialEnv_r( LSAPI_Request * pReq,
169 LSAPI_CB_EnvHandler fn, void * arg );
170
171 char * LSAPI_GetEnv_r( LSAPI_Request * pReq, const char * name );
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 int LSAPI_End_Response_r(LSAPI_Request * pReq);
270
271
272
273 int LSAPI_Is_Listen(void);
274
LSAPI_Accept(void)275 static inline int LSAPI_Accept( void )
276 { return LSAPI_Accept_r( &g_req ); }
277
LSAPI_Finish(void)278 static inline int LSAPI_Finish(void)
279 { return LSAPI_Finish_r( &g_req ); }
280
LSAPI_GetHeader(int headerIndex)281 static inline char * LSAPI_GetHeader( int headerIndex )
282 { return LSAPI_GetHeader_r( &g_req, headerIndex ); }
283
LSAPI_ForeachHeader(LSAPI_CB_EnvHandler fn,void * arg)284 static inline int LSAPI_ForeachHeader( LSAPI_CB_EnvHandler fn, void * arg )
285 { return LSAPI_ForeachHeader_r( &g_req, fn, arg ); }
286
LSAPI_ForeachOrgHeader(LSAPI_CB_EnvHandler fn,void * arg)287 static inline int LSAPI_ForeachOrgHeader(
288 LSAPI_CB_EnvHandler fn, void * arg )
289 { return LSAPI_ForeachOrgHeader_r( &g_req, fn, arg ); }
290
LSAPI_ForeachEnv(LSAPI_CB_EnvHandler fn,void * arg)291 static inline int LSAPI_ForeachEnv( LSAPI_CB_EnvHandler fn, void * arg )
292 { return LSAPI_ForeachEnv_r( &g_req, fn, arg ); }
293
LSAPI_ForeachSpecialEnv(LSAPI_CB_EnvHandler fn,void * arg)294 static inline int LSAPI_ForeachSpecialEnv( LSAPI_CB_EnvHandler fn, void * arg )
295 { return LSAPI_ForeachSpecialEnv_r( &g_req, fn, arg ); }
296
LSAPI_GetEnv(const char * name)297 static inline char * LSAPI_GetEnv( const char * name )
298 { return LSAPI_GetEnv_r( &g_req, name ); }
299
LSAPI_GetQueryString(void)300 static inline char * LSAPI_GetQueryString(void)
301 { return LSAPI_GetQueryString_r( &g_req ); }
302
LSAPI_GetScriptFileName(void)303 static inline char * LSAPI_GetScriptFileName(void)
304 { return LSAPI_GetScriptFileName_r( &g_req ); }
305
LSAPI_GetScriptName(void)306 static inline char * LSAPI_GetScriptName(void)
307 { return LSAPI_GetScriptName_r( &g_req ); }
308
LSAPI_GetRequestMethod(void)309 static inline char * LSAPI_GetRequestMethod(void)
310 { return LSAPI_GetRequestMethod_r( &g_req ); }
311
LSAPI_GetReqBodyLen(void)312 static inline off_t LSAPI_GetReqBodyLen(void)
313 { return LSAPI_GetReqBodyLen_r( &g_req ); }
314
LSAPI_GetReqBodyRemain(void)315 static inline off_t LSAPI_GetReqBodyRemain(void)
316 { return LSAPI_GetReqBodyRemain_r( &g_req ); }
317
LSAPI_ReadReqBody(char * pBuf,size_t len)318 static inline ssize_t LSAPI_ReadReqBody( char * pBuf, size_t len )
319 { return LSAPI_ReadReqBody_r( &g_req, pBuf, len ); }
320
LSAPI_ReqBodyGetChar(void)321 static inline int LSAPI_ReqBodyGetChar(void)
322 { return LSAPI_ReqBodyGetChar_r( &g_req ); }
323
LSAPI_ReqBodyGetLine(char * pBuf,int len,int * getLF)324 static inline int LSAPI_ReqBodyGetLine( char * pBuf, int len, int *getLF )
325 { return LSAPI_ReqBodyGetLine_r( &g_req, pBuf, len, getLF ); }
326
327
328
LSAPI_FinalizeRespHeaders(void)329 static inline int LSAPI_FinalizeRespHeaders(void)
330 { return LSAPI_FinalizeRespHeaders_r( &g_req ); }
331
LSAPI_Write(const char * pBuf,ssize_t len)332 static inline ssize_t LSAPI_Write( const char * pBuf, ssize_t len )
333 { return LSAPI_Write_r( &g_req, pBuf, len ); }
334
LSAPI_sendfile(int fdIn,off_t * off,size_t size)335 static inline ssize_t LSAPI_sendfile( int fdIn, off_t* off, size_t size )
336 {
337 return LSAPI_sendfile_r(&g_req, fdIn, off, size );
338 }
339
LSAPI_Write_Stderr(const char * pBuf,ssize_t len)340 static inline ssize_t LSAPI_Write_Stderr( const char * pBuf, ssize_t len )
341 { return LSAPI_Write_Stderr_r( &g_req, pBuf, len ); }
342
LSAPI_Flush(void)343 static inline int LSAPI_Flush(void)
344 { return LSAPI_Flush_r( &g_req ); }
345
LSAPI_AppendRespHeader(char * pBuf,int len)346 static inline int LSAPI_AppendRespHeader( char * pBuf, int len )
347 { return LSAPI_AppendRespHeader_r( &g_req, pBuf, len ); }
348
LSAPI_SetRespStatus(int code)349 static inline int LSAPI_SetRespStatus( int code )
350 { return LSAPI_SetRespStatus_r( &g_req, code ); }
351
LSAPI_ErrResponse(int code,const char ** pRespHeaders,const char * pBody,int bodyLen)352 static inline int LSAPI_ErrResponse( int code, const char ** pRespHeaders, const char * pBody, int bodyLen )
353 { return LSAPI_ErrResponse_r( &g_req, code, pRespHeaders, pBody, bodyLen ); }
354
LSAPI_End_Response(void)355 static inline int LSAPI_End_Response(void)
356 { return LSAPI_End_Response_r( &g_req ); }
357
358 int LSAPI_IsRunning(void);
359
360 int LSAPI_CreateListenSock( const char * pBind, int backlog );
361
362 typedef int (*fn_select_t)( int, fd_set *, fd_set *, fd_set *, struct timeval * );
363
364 int LSAPI_Init_Prefork_Server( int max_children, fn_select_t fp, int avoidFork );
365
366 void LSAPI_Set_Server_fd( int fd );
367
368 int LSAPI_Prefork_Accept_r( LSAPI_Request * pReq );
369
370 void LSAPI_No_Check_ppid(void);
371
372 void LSAPI_Set_Max_Reqs( int reqs );
373
374 void LSAPI_Set_Max_Idle( int secs );
375
376 void LSAPI_Set_Max_Children( int maxChildren );
377
378 void LSAPI_Set_Max_Idle_Children( int maxIdleChld );
379
380 void LSAPI_Set_Server_Max_Idle_Secs( int serverMaxIdle );
381
382 void LSAPI_Set_Max_Process_Time( int secs );
383
384 int LSAPI_Init_Env_Parameters( fn_select_t fp );
385
386 void LSAPI_Set_Slow_Req_Msecs( int msecs );
387
388 int LSAPI_Get_Slow_Req_Msecs(void);
389
390 int LSAPI_is_suEXEC_Daemon(void);
391
392 int LSAPI_Set_Restored_Parent_Pid(int pid);
393
394 typedef void (*LSAPI_On_Timer_pf)(int *forked_child_pid);
395 void LSAPI_Register_Pgrp_Timer_Callback(LSAPI_On_Timer_pf);
396
397 int LSAPI_Inc_Req_Processed(int cnt);
398
399 int LSAPI_Accept_Before_Fork(LSAPI_Request * pReq);
400
401 int LSAPI_Postfork_Child(LSAPI_Request * pReq);
402
403 int LSAPI_Postfork_Parent(LSAPI_Request * pReq);
404
405 #define LSAPI_LOG_LEVEL_BITS 0xff
406 #define LSAPI_LOG_FLAG_NONE 0
407 #define LSAPI_LOG_FLAG_DEBUG 1
408 #define LSAPI_LOG_FLAG_INFO 2
409 #define LSAPI_LOG_FLAG_NOTICE 3
410 #define LSAPI_LOG_FLAG_WARN 4
411 #define LSAPI_LOG_FLAG_ERROR 5
412 #define LSAPI_LOG_FLAG_CRIT 6
413 #define LSAPI_LOG_FLAG_FATAL 7
414
415 #define LSAPI_LOG_TIMESTAMP_BITS (0xff00)
416 #define LSAPI_LOG_TIMESTAMP_FULL (0x100)
417 #define LSAPI_LOG_TIMESTAMP_HMS (0x200)
418 #define LSAPI_LOG_TIMESTAMP_STDERR (0x400)
419
420 #define LSAPI_LOG_PID (0x10000)
421
422 void LSAPI_Log(int flag, const char * fmt, ...)
423 #if __GNUC__
424 __attribute__((format(printf, 2, 3)))
425 #endif
426 ;
427
428
429 #if defined (c_plusplus) || defined (__cplusplus)
430 }
431 #endif
432
433
434 #endif
435