xref: /PHP-7.2/sapi/litespeed/lsapilib.h (revision 32af676b)
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 
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 
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 ssize_t LSAPI_ReadReqBody_r( LSAPI_Request * pReq, char * pBuf, size_t len );
173 
174 int LSAPI_ReqBodyGetChar_r( LSAPI_Request * pReq );
175 
176 int LSAPI_ReqBodyGetLine_r( LSAPI_Request * pReq, char * pBuf, size_t bufLen, int *getLF );
177 
178 
179 int LSAPI_FinalizeRespHeaders_r( LSAPI_Request * pReq );
180 
181 ssize_t LSAPI_Write_r( LSAPI_Request * pReq, const char * pBuf, size_t len );
182 
183 ssize_t LSAPI_sendfile_r( LSAPI_Request * pReq, int fdIn, off_t* off, size_t size );
184 
185 ssize_t LSAPI_Write_Stderr_r( LSAPI_Request * pReq, const char * pBuf, size_t len );
186 
187 int LSAPI_Flush_r( LSAPI_Request * pReq );
188 
189 int LSAPI_AppendRespHeader_r( LSAPI_Request * pReq, const char * pBuf, int len );
190 
191 int LSAPI_AppendRespHeader2_r( LSAPI_Request * pReq, const char * pHeaderName,
192                               const char * pHeaderValue );
193 
194 int LSAPI_ErrResponse_r( LSAPI_Request * pReq, int code, const char ** pRespHeaders,
195                          const char * pBody, int bodyLen );
196 
LSAPI_SetRespStatus_r(LSAPI_Request * pReq,int code)197 static inline int LSAPI_SetRespStatus_r( LSAPI_Request * pReq, int code )
198 {
199     if ( !pReq )
200         return -1;
201     pReq->m_respHeader.m_respInfo.m_status = code;
202     return 0;
203 }
204 
LSAPI_SetAppData_r(LSAPI_Request * pReq,void * data)205 static inline int LSAPI_SetAppData_r( LSAPI_Request * pReq, void * data )
206 {
207     if ( !pReq )
208         return -1;
209     pReq->m_pAppData = data;
210     return 0;
211 }
212 
LSAPI_GetAppData_r(LSAPI_Request * pReq)213 static inline void * LSAPI_GetAppData_r( LSAPI_Request * pReq )
214 {
215     if ( !pReq )
216         return NULL;
217     return pReq->m_pAppData;
218 }
219 
LSAPI_GetQueryString_r(LSAPI_Request * pReq)220 static inline char * LSAPI_GetQueryString_r( LSAPI_Request * pReq )
221 {
222     if ( pReq )
223         return pReq->m_pQueryString;
224     return NULL;
225 }
226 
227 
LSAPI_GetScriptFileName_r(LSAPI_Request * pReq)228 static inline char * LSAPI_GetScriptFileName_r( LSAPI_Request * pReq )
229 {
230     if ( pReq )
231         return pReq->m_pScriptFile;
232     return NULL;
233 }
234 
235 
LSAPI_GetScriptName_r(LSAPI_Request * pReq)236 static inline char * LSAPI_GetScriptName_r( LSAPI_Request * pReq )
237 {
238     if ( pReq )
239         return pReq->m_pScriptName;
240     return NULL;
241 }
242 
243 
LSAPI_GetRequestMethod_r(LSAPI_Request * pReq)244 static inline char * LSAPI_GetRequestMethod_r( LSAPI_Request * pReq)
245 {
246     if ( pReq )
247         return pReq->m_pRequestMethod;
248     return NULL;
249 }
250 
251 
252 
LSAPI_GetReqBodyLen_r(LSAPI_Request * pReq)253 static inline off_t LSAPI_GetReqBodyLen_r( LSAPI_Request * pReq )
254 {
255     if ( pReq )
256         return pReq->m_reqBodyLen;
257     return -1;
258 }
259 
LSAPI_GetReqBodyRemain_r(LSAPI_Request * pReq)260 static inline off_t LSAPI_GetReqBodyRemain_r( LSAPI_Request * pReq )
261 {
262     if ( pReq )
263         return pReq->m_reqBodyLen - pReq->m_reqBodyRead;
264     return -1;
265 }
266 
267 
268 int LSAPI_End_Response_r(LSAPI_Request * pReq);
269 
270 
271 
272 int LSAPI_Is_Listen(void);
273 
LSAPI_Accept(void)274 static inline int LSAPI_Accept( void )
275 {   return LSAPI_Accept_r( &g_req );                        }
276 
LSAPI_Finish(void)277 static inline int LSAPI_Finish(void)
278 {   return LSAPI_Finish_r( &g_req );                        }
279 
LSAPI_GetHeader(int headerIndex)280 static inline char * LSAPI_GetHeader( int headerIndex )
281 {   return LSAPI_GetHeader_r( &g_req, headerIndex );        }
282 
LSAPI_ForeachHeader(LSAPI_CB_EnvHandler fn,void * arg)283 static inline int LSAPI_ForeachHeader( LSAPI_CB_EnvHandler fn, void * arg )
284 {   return LSAPI_ForeachHeader_r( &g_req, fn, arg );        }
285 
LSAPI_ForeachOrgHeader(LSAPI_CB_EnvHandler fn,void * arg)286 static inline int LSAPI_ForeachOrgHeader(
287             LSAPI_CB_EnvHandler fn, void * arg )
288 {   return LSAPI_ForeachOrgHeader_r( &g_req, fn, arg );     }
289 
LSAPI_ForeachEnv(LSAPI_CB_EnvHandler fn,void * arg)290 static inline int LSAPI_ForeachEnv( LSAPI_CB_EnvHandler fn, void * arg )
291 {   return LSAPI_ForeachEnv_r( &g_req, fn, arg );           }
292 
LSAPI_ForeachSpecialEnv(LSAPI_CB_EnvHandler fn,void * arg)293 static inline int LSAPI_ForeachSpecialEnv( LSAPI_CB_EnvHandler fn, void * arg )
294 {   return LSAPI_ForeachSpecialEnv_r( &g_req, fn, arg );    }
295 
LSAPI_GetEnv(const char * name)296 static inline char * LSAPI_GetEnv( const char * name )
297 {   return LSAPI_GetEnv_r( &g_req, name );                  }
298 
LSAPI_GetQueryString(void)299 static inline char * LSAPI_GetQueryString(void)
300 {   return LSAPI_GetQueryString_r( &g_req );                }
301 
LSAPI_GetScriptFileName(void)302 static inline char * LSAPI_GetScriptFileName(void)
303 {   return LSAPI_GetScriptFileName_r( &g_req );             }
304 
LSAPI_GetScriptName(void)305 static inline char * LSAPI_GetScriptName(void)
306 {    return LSAPI_GetScriptName_r( &g_req );                }
307 
LSAPI_GetRequestMethod(void)308 static inline char * LSAPI_GetRequestMethod(void)
309 {   return LSAPI_GetRequestMethod_r( &g_req );              }
310 
LSAPI_GetReqBodyLen(void)311 static inline off_t LSAPI_GetReqBodyLen(void)
312 {   return LSAPI_GetReqBodyLen_r( &g_req );                 }
313 
LSAPI_GetReqBodyRemain(void)314 static inline off_t LSAPI_GetReqBodyRemain(void)
315 {   return LSAPI_GetReqBodyRemain_r( &g_req );                 }
316 
LSAPI_ReadReqBody(char * pBuf,size_t len)317 static inline ssize_t LSAPI_ReadReqBody( char * pBuf, size_t len )
318 {   return LSAPI_ReadReqBody_r( &g_req, pBuf, len );        }
319 
LSAPI_ReqBodyGetChar(void)320 static inline int LSAPI_ReqBodyGetChar(void)
321 {   return LSAPI_ReqBodyGetChar_r( &g_req );        }
322 
LSAPI_ReqBodyGetLine(char * pBuf,int len,int * getLF)323 static inline int LSAPI_ReqBodyGetLine( char * pBuf, int len, int *getLF )
324 {   return LSAPI_ReqBodyGetLine_r( &g_req, pBuf, len, getLF );        }
325 
326 
327 
LSAPI_FinalizeRespHeaders(void)328 static inline int LSAPI_FinalizeRespHeaders(void)
329 {   return LSAPI_FinalizeRespHeaders_r( &g_req );           }
330 
LSAPI_Write(const char * pBuf,ssize_t len)331 static inline ssize_t LSAPI_Write( const char * pBuf, ssize_t len )
332 {   return LSAPI_Write_r( &g_req, pBuf, len );              }
333 
LSAPI_sendfile(int fdIn,off_t * off,size_t size)334 static inline ssize_t LSAPI_sendfile( int fdIn, off_t* off, size_t size )
335 {
336     return LSAPI_sendfile_r(&g_req, fdIn, off, size );
337 }
338 
LSAPI_Write_Stderr(const char * pBuf,ssize_t len)339 static inline ssize_t LSAPI_Write_Stderr( const char * pBuf, ssize_t len )
340 {   return LSAPI_Write_Stderr_r( &g_req, pBuf, len );       }
341 
LSAPI_Flush(void)342 static inline int LSAPI_Flush(void)
343 {   return LSAPI_Flush_r( &g_req );                         }
344 
LSAPI_AppendRespHeader(char * pBuf,int len)345 static inline int LSAPI_AppendRespHeader( char * pBuf, int len )
346 {   return LSAPI_AppendRespHeader_r( &g_req, pBuf, len );   }
347 
LSAPI_SetRespStatus(int code)348 static inline int LSAPI_SetRespStatus( int code )
349 {   return LSAPI_SetRespStatus_r( &g_req, code );           }
350 
LSAPI_ErrResponse(int code,const char ** pRespHeaders,const char * pBody,int bodyLen)351 static inline int LSAPI_ErrResponse( int code, const char ** pRespHeaders, const char * pBody, int bodyLen )
352 {   return LSAPI_ErrResponse_r( &g_req, code, pRespHeaders, pBody, bodyLen );   }
353 
LSAPI_End_Response(void)354 static inline int LSAPI_End_Response(void)
355 {   return LSAPI_End_Response_r( &g_req );                         }
356 
357 int LSAPI_IsRunning(void);
358 
359 int LSAPI_CreateListenSock( const char * pBind, int backlog );
360 
361 typedef int (*fn_select_t)( int, fd_set *, fd_set *, fd_set *, struct timeval * );
362 
363 int LSAPI_Init_Prefork_Server( int max_children, fn_select_t fp, int avoidFork );
364 
365 void LSAPI_Set_Server_fd( int fd );
366 
367 int LSAPI_Prefork_Accept_r( LSAPI_Request * pReq );
368 
369 void LSAPI_No_Check_ppid(void);
370 
371 void LSAPI_Set_Max_Reqs( int reqs );
372 
373 void LSAPI_Set_Max_Idle( int secs );
374 
375 void LSAPI_Set_Max_Children( int maxChildren );
376 
377 void LSAPI_Set_Max_Idle_Children( int maxIdleChld );
378 
379 void LSAPI_Set_Server_Max_Idle_Secs( int serverMaxIdle );
380 
381 void LSAPI_Set_Max_Process_Time( int secs );
382 
383 int LSAPI_Init_Env_Parameters( fn_select_t fp );
384 
385 void LSAPI_Set_Slow_Req_Msecs( int msecs );
386 
387 int  LSAPI_Get_Slow_Req_Msecs(void);
388 
389 int LSAPI_is_suEXEC_Daemon(void);
390 
391 int LSAPI_Set_Restored_Parent_Pid(int pid);
392 
393 typedef void (*LSAPI_On_Timer_pf)(int *forked_child_pid);
394 void LSAPI_Register_Pgrp_Timer_Callback(LSAPI_On_Timer_pf);
395 
396 int LSAPI_Inc_Req_Processed(int cnt);
397 
398 #define LSAPI_LOG_LEVEL_BITS    0xff
399 #define LSAPI_LOG_FLAG_NONE     0
400 #define LSAPI_LOG_FLAG_DEBUG    1
401 #define LSAPI_LOG_FLAG_INFO     2
402 #define LSAPI_LOG_FLAG_NOTICE   3
403 #define LSAPI_LOG_FLAG_WARN     4
404 #define LSAPI_LOG_FLAG_ERROR    5
405 #define LSAPI_LOG_FLAG_CRIT     6
406 #define LSAPI_LOG_FLAG_FATAL    7
407 
408 #define LSAPI_LOG_TIMESTAMP_BITS (0xff00)
409 #define LSAPI_LOG_TIMESTAMP_FULL (0x100)
410 #define LSAPI_LOG_TIMESTAMP_HMS  (0x200)
411 #define LSAPI_LOG_TIMESTAMP_STDERR  (0x400)
412 
413 #define LSAPI_LOG_PID            (0x10000)
414 
415 void LSAPI_Log(int flag, const char * fmt, ...)
416 #if __GNUC__
417         __attribute__((format(printf, 2, 3)))
418 #endif
419 ;
420 
421 
422 #if defined (c_plusplus) || defined (__cplusplus)
423 }
424 #endif
425 
426 
427 #endif
428