xref: /PHP-7.1/sapi/litespeed/lsapilib.h (revision 03f3b847)
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  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 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 
269 int LSAPI_Is_Listen(void);
270 
LSAPI_Accept(void)271 static inline int LSAPI_Accept( void )
272 {   return LSAPI_Accept_r( &g_req );                        }
273 
LSAPI_Finish(void)274 static inline int LSAPI_Finish(void)
275 {   return LSAPI_Finish_r( &g_req );                        }
276 
LSAPI_GetHeader(int headerIndex)277 static inline char * LSAPI_GetHeader( int headerIndex )
278 {   return LSAPI_GetHeader_r( &g_req, headerIndex );        }
279 
LSAPI_ForeachHeader(LSAPI_CB_EnvHandler fn,void * arg)280 static inline int LSAPI_ForeachHeader( LSAPI_CB_EnvHandler fn, void * arg )
281 {   return LSAPI_ForeachHeader_r( &g_req, fn, arg );        }
282 
LSAPI_ForeachOrgHeader(LSAPI_CB_EnvHandler fn,void * arg)283 static inline int LSAPI_ForeachOrgHeader(
284             LSAPI_CB_EnvHandler fn, void * arg )
285 {   return LSAPI_ForeachOrgHeader_r( &g_req, fn, arg );     }
286 
LSAPI_ForeachEnv(LSAPI_CB_EnvHandler fn,void * arg)287 static inline int LSAPI_ForeachEnv( LSAPI_CB_EnvHandler fn, void * arg )
288 {   return LSAPI_ForeachEnv_r( &g_req, fn, arg );           }
289 
LSAPI_ForeachSpecialEnv(LSAPI_CB_EnvHandler fn,void * arg)290 static inline int LSAPI_ForeachSpecialEnv( LSAPI_CB_EnvHandler fn, void * arg )
291 {   return LSAPI_ForeachSpecialEnv_r( &g_req, fn, arg );    }
292 
LSAPI_GetEnv(const char * name)293 static inline char * LSAPI_GetEnv( const char * name )
294 {   return LSAPI_GetEnv_r( &g_req, name );                  }
295 
LSAPI_GetQueryString(void)296 static inline char * LSAPI_GetQueryString(void)
297 {   return LSAPI_GetQueryString_r( &g_req );                }
298 
LSAPI_GetScriptFileName(void)299 static inline char * LSAPI_GetScriptFileName(void)
300 {   return LSAPI_GetScriptFileName_r( &g_req );             }
301 
LSAPI_GetScriptName(void)302 static inline char * LSAPI_GetScriptName(void)
303 {    return LSAPI_GetScriptName_r( &g_req );                }
304 
LSAPI_GetRequestMethod(void)305 static inline char * LSAPI_GetRequestMethod(void)
306 {   return LSAPI_GetRequestMethod_r( &g_req );              }
307 
LSAPI_GetReqBodyLen(void)308 static inline off_t LSAPI_GetReqBodyLen(void)
309 {   return LSAPI_GetReqBodyLen_r( &g_req );                 }
310 
LSAPI_GetReqBodyRemain(void)311 static inline off_t LSAPI_GetReqBodyRemain(void)
312 {   return LSAPI_GetReqBodyRemain_r( &g_req );                 }
313 
LSAPI_ReadReqBody(char * pBuf,size_t len)314 static inline ssize_t LSAPI_ReadReqBody( char * pBuf, size_t len )
315 {   return LSAPI_ReadReqBody_r( &g_req, pBuf, len );        }
316 
LSAPI_ReqBodyGetChar(void)317 static inline int LSAPI_ReqBodyGetChar(void)
318 {   return LSAPI_ReqBodyGetChar_r( &g_req );        }
319 
LSAPI_ReqBodyGetLine(char * pBuf,int len,int * getLF)320 static inline int LSAPI_ReqBodyGetLine( char * pBuf, int len, int *getLF )
321 {   return LSAPI_ReqBodyGetLine_r( &g_req, pBuf, len, getLF );        }
322 
323 
324 
LSAPI_FinalizeRespHeaders(void)325 static inline int LSAPI_FinalizeRespHeaders(void)
326 {   return LSAPI_FinalizeRespHeaders_r( &g_req );           }
327 
LSAPI_Write(const char * pBuf,ssize_t len)328 static inline ssize_t LSAPI_Write( const char * pBuf, ssize_t len )
329 {   return LSAPI_Write_r( &g_req, pBuf, len );              }
330 
LSAPI_sendfile(int fdIn,off_t * off,size_t size)331 static inline ssize_t LSAPI_sendfile( int fdIn, off_t* off, size_t size )
332 {
333     return LSAPI_sendfile_r(&g_req, fdIn, off, size );
334 }
335 
LSAPI_Write_Stderr(const char * pBuf,ssize_t len)336 static inline ssize_t LSAPI_Write_Stderr( const char * pBuf, ssize_t len )
337 {   return LSAPI_Write_Stderr_r( &g_req, pBuf, len );       }
338 
LSAPI_Flush(void)339 static inline int LSAPI_Flush(void)
340 {   return LSAPI_Flush_r( &g_req );                         }
341 
LSAPI_AppendRespHeader(char * pBuf,int len)342 static inline int LSAPI_AppendRespHeader( char * pBuf, int len )
343 {   return LSAPI_AppendRespHeader_r( &g_req, pBuf, len );   }
344 
LSAPI_SetRespStatus(int code)345 static inline int LSAPI_SetRespStatus( int code )
346 {   return LSAPI_SetRespStatus_r( &g_req, code );           }
347 
LSAPI_ErrResponse(int code,const char ** pRespHeaders,const char * pBody,int bodyLen)348 static inline int LSAPI_ErrResponse( int code, const char ** pRespHeaders, const char * pBody, int bodyLen )
349 {   return LSAPI_ErrResponse_r( &g_req, code, pRespHeaders, pBody, bodyLen );   }
350 
351 int LSAPI_IsRunning(void);
352 
353 int LSAPI_CreateListenSock( const char * pBind, int backlog );
354 
355 typedef int (*fn_select_t)( int, fd_set *, fd_set *, fd_set *, struct timeval * );
356 
357 int LSAPI_Init_Prefork_Server( int max_children, fn_select_t fp, int avoidFork );
358 
359 void LSAPI_Set_Server_fd( int fd );
360 
361 int LSAPI_Prefork_Accept_r( LSAPI_Request * pReq );
362 
363 void LSAPI_No_Check_ppid(void);
364 
365 void LSAPI_Set_Max_Reqs( int reqs );
366 
367 void LSAPI_Set_Max_Idle( int secs );
368 
369 void LSAPI_Set_Max_Children( int maxChildren );
370 
371 void LSAPI_Set_Max_Idle_Children( int maxIdleChld );
372 
373 void LSAPI_Set_Server_Max_Idle_Secs( int serverMaxIdle );
374 
375 void LSAPI_Set_Max_Process_Time( int secs );
376 
377 int LSAPI_Init_Env_Parameters( fn_select_t fp );
378 
379 void LSAPI_Set_Slow_Req_Msecs( int msecs );
380 
381 int  LSAPI_Get_Slow_Req_Msecs(void);
382 
383 int LSAPI_is_suEXEC_Daemon(void);
384 
385 int LSAPI_Set_Restored_Parent_Pid(int pid);
386 
387 typedef void (*LSAPI_On_Timer_pf)(int *forked_child_pid);
388 void LSAPI_Register_Pgrp_Timer_Callback(LSAPI_On_Timer_pf);
389 
390 int LSAPI_Inc_Req_Processed(int cnt);
391 
392 #define LSAPI_LOG_LEVEL_BITS    0xff
393 #define LSAPI_LOG_FLAG_NONE     0
394 #define LSAPI_LOG_FLAG_DEBUG    1
395 #define LSAPI_LOG_FLAG_INFO     2
396 #define LSAPI_LOG_FLAG_NOTICE   3
397 #define LSAPI_LOG_FLAG_WARN     4
398 #define LSAPI_LOG_FLAG_ERROR    5
399 #define LSAPI_LOG_FLAG_CRIT     6
400 #define LSAPI_LOG_FLAG_FATAL    7
401 
402 #define LSAPI_LOG_TIMESTAMP_BITS (0xff00)
403 #define LSAPI_LOG_TIMESTAMP_FULL (0x100)
404 #define LSAPI_LOG_TIMESTAMP_HMS  (0x200)
405 
406 #define LSAPI_LOG_PID            (0x10000)
407 
408 void LSAPI_Log(int flag, const char * fmt, ...)
409 #if __GNUC__
410         __attribute__((format(printf, 2, 3)))
411 #endif
412 ;
413 
414 
415 #if defined (c_plusplus) || defined (__cplusplus)
416 }
417 #endif
418 
419 
420 #endif
421