xref: /php-src/sapi/litespeed/lsapilib.h (revision cf3b9fca)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Author: George Wang <gwang@litespeedtech.com>                        |
14    +----------------------------------------------------------------------+
15 */
16 
17 /*
18 Copyright (c) 2002-2018, Lite Speed Technologies Inc.
19 All rights reserved.
20 
21 Redistribution and use in source and binary forms, with or without
22 modification, are permitted provided that the following conditions are
23 met:
24 
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 
36 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
37 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
39 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
40 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
43 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
44 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
45 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
46 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 */
48 
49 
50 #ifndef  _LSAPILIB_H_
51 #define  _LSAPILIB_H_
52 
53 #if defined (c_plusplus) || defined (__cplusplus)
54 extern "C" {
55 #endif
56 
57 #include "lsapidef.h"
58 
59 #include <stddef.h>
60 #include <sys/time.h>
61 #include <sys/types.h>
62 
63 struct LSAPI_key_value_pair
64 {
65     char * pKey;
66     char * pValue;
67     int    keyLen;
68     int    valLen;
69 };
70 
71 struct lsapi_child_status;
72 #define LSAPI_MAX_RESP_HEADERS  1000
73 
74 typedef struct lsapi_request
75 {
76     int               m_fdListen;
77     int               m_fd;
78 
79     long              m_lLastActive;
80     long              m_lReqBegin;
81 
82     char            * m_pReqBuf;
83     int               m_reqBufSize;
84 
85     char            * m_pRespBuf;
86     char            * m_pRespBufEnd;
87     char            * m_pRespBufPos;
88 
89     char            * m_pRespHeaderBuf;
90     char            * m_pRespHeaderBufEnd;
91     char            * m_pRespHeaderBufPos;
92     struct lsapi_child_status * child_status;
93 
94 
95     struct iovec    * m_pIovec;
96     struct iovec    * m_pIovecEnd;
97     struct iovec    * m_pIovecCur;
98     struct iovec    * m_pIovecToWrite;
99 
100     struct lsapi_packet_header      * m_respPktHeaderEnd;
101 
102     struct lsapi_req_header         * m_pHeader;
103     struct LSAPI_key_value_pair     * m_pEnvList;
104     struct LSAPI_key_value_pair     * m_pSpecialEnvList;
105     int                               m_envListSize;
106     int                               m_specialEnvListSize;
107 
108     struct lsapi_http_header_index  * m_pHeaderIndex;
109     struct lsapi_header_offset      * m_pUnknownHeader;
110 
111     char            * m_pScriptFile;
112     char            * m_pScriptName;
113     char            * m_pQueryString;
114     char            * m_pHttpHeader;
115     char            * m_pRequestMethod;
116     int               m_totalLen;
117     int               m_reqState;
118     off_t             m_reqBodyLen;
119     off_t             m_reqBodyRead;
120     int               m_bufProcessed;
121     int               m_bufRead;
122 
123     struct lsapi_packet_header        m_respPktHeader[5];
124 
125     struct lsapi_resp_header          m_respHeader;
126     short                             m_respHeaderLen[LSAPI_MAX_RESP_HEADERS];
127     void            * m_pAppData;
128 
129 }LSAPI_Request;
130 
131 extern LSAPI_Request g_req;
132 
133 
134 /* return: >0 continue, ==0 stop, -1 failed  */
135 typedef int (*LSAPI_CB_EnvHandler )( const char * pKey, int keyLen,
136                 const char * pValue, int valLen, void * arg );
137 
138 
139 int LSAPI_Init(void);
140 
141 void LSAPI_Stop(void);
142 
143 int LSAPI_Is_Listen_r( LSAPI_Request * pReq);
144 
145 int LSAPI_InitRequest( LSAPI_Request * pReq, int fd );
146 
147 int LSAPI_Accept_r( LSAPI_Request * pReq );
148 
149 void LSAPI_Reset_r( LSAPI_Request * pReq );
150 
151 int LSAPI_Finish_r( LSAPI_Request * pReq );
152 
153 int LSAPI_Release_r( LSAPI_Request * pReq );
154 
155 char * LSAPI_GetHeader_r( LSAPI_Request * pReq, int headerIndex );
156 
157 int LSAPI_ForeachHeader_r( LSAPI_Request * pReq,
158             LSAPI_CB_EnvHandler fn, void * arg );
159 
160 int LSAPI_ForeachOrgHeader_r( LSAPI_Request * pReq,
161             LSAPI_CB_EnvHandler fn, void * arg );
162 
163 int LSAPI_ForeachEnv_r( LSAPI_Request * pReq,
164             LSAPI_CB_EnvHandler fn, void * arg );
165 
166 int LSAPI_ForeachSpecialEnv_r( LSAPI_Request * pReq,
167             LSAPI_CB_EnvHandler fn, void * arg );
168 
169 char * LSAPI_GetEnv_r( LSAPI_Request * pReq, const char * name );
170 
171 ssize_t LSAPI_ReadReqBody_r( LSAPI_Request * pReq, char * pBuf, size_t len );
172 
173 int LSAPI_ReqBodyGetChar_r( LSAPI_Request * pReq );
174 
175 int LSAPI_ReqBodyGetLine_r( LSAPI_Request * pReq, char * pBuf, size_t bufLen, int *getLF );
176 
177 
178 int LSAPI_FinalizeRespHeaders_r( LSAPI_Request * pReq );
179 
180 ssize_t LSAPI_Write_r( LSAPI_Request * pReq, const char * pBuf, size_t len );
181 
182 ssize_t LSAPI_sendfile_r( LSAPI_Request * pReq, int fdIn, off_t* off, size_t size );
183 
184 ssize_t LSAPI_Write_Stderr_r( LSAPI_Request * pReq, const char * pBuf, size_t len );
185 
186 int LSAPI_Flush_r( LSAPI_Request * pReq );
187 
188 int LSAPI_AppendRespHeader_r( LSAPI_Request * pReq, const char * pBuf, int len );
189 
190 int LSAPI_AppendRespHeader2_r( LSAPI_Request * pReq, const char * pHeaderName,
191                               const char * pHeaderValue );
192 
193 int LSAPI_ErrResponse_r( LSAPI_Request * pReq, int code, const char ** pRespHeaders,
194                          const char * pBody, int bodyLen );
195 
LSAPI_SetRespStatus_r(LSAPI_Request * pReq,int code)196 static inline int LSAPI_SetRespStatus_r( LSAPI_Request * pReq, int code )
197 {
198     if ( !pReq )
199         return -1;
200     pReq->m_respHeader.m_respInfo.m_status = code;
201     return 0;
202 }
203 
LSAPI_SetAppData_r(LSAPI_Request * pReq,void * data)204 static inline int LSAPI_SetAppData_r( LSAPI_Request * pReq, void * data )
205 {
206     if ( !pReq )
207         return -1;
208     pReq->m_pAppData = data;
209     return 0;
210 }
211 
LSAPI_GetAppData_r(LSAPI_Request * pReq)212 static inline void * LSAPI_GetAppData_r( LSAPI_Request * pReq )
213 {
214     if ( !pReq )
215         return NULL;
216     return pReq->m_pAppData;
217 }
218 
LSAPI_GetQueryString_r(LSAPI_Request * pReq)219 static inline char * LSAPI_GetQueryString_r( LSAPI_Request * pReq )
220 {
221     if ( pReq )
222         return pReq->m_pQueryString;
223     return NULL;
224 }
225 
226 
LSAPI_GetScriptFileName_r(LSAPI_Request * pReq)227 static inline char * LSAPI_GetScriptFileName_r( LSAPI_Request * pReq )
228 {
229     if ( pReq )
230         return pReq->m_pScriptFile;
231     return NULL;
232 }
233 
234 
LSAPI_GetScriptName_r(LSAPI_Request * pReq)235 static inline char * LSAPI_GetScriptName_r( LSAPI_Request * pReq )
236 {
237     if ( pReq )
238         return pReq->m_pScriptName;
239     return NULL;
240 }
241 
242 
LSAPI_GetRequestMethod_r(LSAPI_Request * pReq)243 static inline char * LSAPI_GetRequestMethod_r( LSAPI_Request * pReq)
244 {
245     if ( pReq )
246         return pReq->m_pRequestMethod;
247     return NULL;
248 }
249 
250 
251 
LSAPI_GetReqBodyLen_r(LSAPI_Request * pReq)252 static inline off_t LSAPI_GetReqBodyLen_r( LSAPI_Request * pReq )
253 {
254     if ( pReq )
255         return pReq->m_reqBodyLen;
256     return -1;
257 }
258 
LSAPI_GetReqBodyRemain_r(LSAPI_Request * pReq)259 static inline off_t LSAPI_GetReqBodyRemain_r( LSAPI_Request * pReq )
260 {
261     if ( pReq )
262         return pReq->m_reqBodyLen - pReq->m_reqBodyRead;
263     return -1;
264 }
265 
266 
267 int LSAPI_End_Response_r(LSAPI_Request * pReq);
268 
269 
270 
271 int LSAPI_Is_Listen(void);
272 
LSAPI_Accept(void)273 static inline int LSAPI_Accept( void )
274 {   return LSAPI_Accept_r( &g_req );                        }
275 
LSAPI_Finish(void)276 static inline int LSAPI_Finish(void)
277 {   return LSAPI_Finish_r( &g_req );                        }
278 
LSAPI_GetHeader(int headerIndex)279 static inline char * LSAPI_GetHeader( int headerIndex )
280 {   return LSAPI_GetHeader_r( &g_req, headerIndex );        }
281 
LSAPI_ForeachHeader(LSAPI_CB_EnvHandler fn,void * arg)282 static inline int LSAPI_ForeachHeader( LSAPI_CB_EnvHandler fn, void * arg )
283 {   return LSAPI_ForeachHeader_r( &g_req, fn, arg );        }
284 
LSAPI_ForeachOrgHeader(LSAPI_CB_EnvHandler fn,void * arg)285 static inline int LSAPI_ForeachOrgHeader(
286             LSAPI_CB_EnvHandler fn, void * arg )
287 {   return LSAPI_ForeachOrgHeader_r( &g_req, fn, arg );     }
288 
LSAPI_ForeachEnv(LSAPI_CB_EnvHandler fn,void * arg)289 static inline int LSAPI_ForeachEnv( LSAPI_CB_EnvHandler fn, void * arg )
290 {   return LSAPI_ForeachEnv_r( &g_req, fn, arg );           }
291 
LSAPI_ForeachSpecialEnv(LSAPI_CB_EnvHandler fn,void * arg)292 static inline int LSAPI_ForeachSpecialEnv( LSAPI_CB_EnvHandler fn, void * arg )
293 {   return LSAPI_ForeachSpecialEnv_r( &g_req, fn, arg );    }
294 
LSAPI_GetEnv(const char * name)295 static inline char * LSAPI_GetEnv( const char * name )
296 {   return LSAPI_GetEnv_r( &g_req, name );                  }
297 
LSAPI_GetQueryString(void)298 static inline char * LSAPI_GetQueryString(void)
299 {   return LSAPI_GetQueryString_r( &g_req );                }
300 
LSAPI_GetScriptFileName(void)301 static inline char * LSAPI_GetScriptFileName(void)
302 {   return LSAPI_GetScriptFileName_r( &g_req );             }
303 
LSAPI_GetScriptName(void)304 static inline char * LSAPI_GetScriptName(void)
305 {    return LSAPI_GetScriptName_r( &g_req );                }
306 
LSAPI_GetRequestMethod(void)307 static inline char * LSAPI_GetRequestMethod(void)
308 {   return LSAPI_GetRequestMethod_r( &g_req );              }
309 
LSAPI_GetReqBodyLen(void)310 static inline off_t LSAPI_GetReqBodyLen(void)
311 {   return LSAPI_GetReqBodyLen_r( &g_req );                 }
312 
LSAPI_GetReqBodyRemain(void)313 static inline off_t LSAPI_GetReqBodyRemain(void)
314 {   return LSAPI_GetReqBodyRemain_r( &g_req );                 }
315 
LSAPI_ReadReqBody(char * pBuf,size_t len)316 static inline ssize_t LSAPI_ReadReqBody( char * pBuf, size_t len )
317 {   return LSAPI_ReadReqBody_r( &g_req, pBuf, len );        }
318 
LSAPI_ReqBodyGetChar(void)319 static inline int LSAPI_ReqBodyGetChar(void)
320 {   return LSAPI_ReqBodyGetChar_r( &g_req );        }
321 
LSAPI_ReqBodyGetLine(char * pBuf,int len,int * getLF)322 static inline int LSAPI_ReqBodyGetLine( char * pBuf, int len, int *getLF )
323 {   return LSAPI_ReqBodyGetLine_r( &g_req, pBuf, len, getLF );        }
324 
325 
326 
LSAPI_FinalizeRespHeaders(void)327 static inline int LSAPI_FinalizeRespHeaders(void)
328 {   return LSAPI_FinalizeRespHeaders_r( &g_req );           }
329 
LSAPI_Write(const char * pBuf,ssize_t len)330 static inline ssize_t LSAPI_Write( const char * pBuf, ssize_t len )
331 {   return LSAPI_Write_r( &g_req, pBuf, len );              }
332 
LSAPI_sendfile(int fdIn,off_t * off,size_t size)333 static inline ssize_t LSAPI_sendfile( int fdIn, off_t* off, size_t size )
334 {
335     return LSAPI_sendfile_r(&g_req, fdIn, off, size );
336 }
337 
LSAPI_Write_Stderr(const char * pBuf,ssize_t len)338 static inline ssize_t LSAPI_Write_Stderr( const char * pBuf, ssize_t len )
339 {   return LSAPI_Write_Stderr_r( &g_req, pBuf, len );       }
340 
LSAPI_Flush(void)341 static inline int LSAPI_Flush(void)
342 {   return LSAPI_Flush_r( &g_req );                         }
343 
LSAPI_AppendRespHeader(char * pBuf,int len)344 static inline int LSAPI_AppendRespHeader( char * pBuf, int len )
345 {   return LSAPI_AppendRespHeader_r( &g_req, pBuf, len );   }
346 
LSAPI_SetRespStatus(int code)347 static inline int LSAPI_SetRespStatus( int code )
348 {   return LSAPI_SetRespStatus_r( &g_req, code );           }
349 
LSAPI_ErrResponse(int code,const char ** pRespHeaders,const char * pBody,int bodyLen)350 static inline int LSAPI_ErrResponse( int code, const char ** pRespHeaders, const char * pBody, int bodyLen )
351 {   return LSAPI_ErrResponse_r( &g_req, code, pRespHeaders, pBody, bodyLen );   }
352 
LSAPI_End_Response(void)353 static inline int LSAPI_End_Response(void)
354 {   return LSAPI_End_Response_r( &g_req );                         }
355 
356 int LSAPI_IsRunning(void);
357 
358 int LSAPI_CreateListenSock( const char * pBind, int backlog );
359 
360 typedef int (*fn_select_t)( int, fd_set *, fd_set *, fd_set *, struct timeval * );
361 
362 int LSAPI_Init_Prefork_Server( int max_children, fn_select_t fp, int avoidFork );
363 
364 void LSAPI_Set_Server_fd( int fd );
365 
366 int LSAPI_Prefork_Accept_r( LSAPI_Request * pReq );
367 
368 void LSAPI_No_Check_ppid(void);
369 
370 void LSAPI_Set_Max_Reqs( int reqs );
371 
372 void LSAPI_Set_Max_Idle( int secs );
373 
374 void LSAPI_Set_Max_Children( int maxChildren );
375 
376 void LSAPI_Set_Max_Idle_Children( int maxIdleChld );
377 
378 void LSAPI_Set_Server_Max_Idle_Secs( int serverMaxIdle );
379 
380 void LSAPI_Set_Max_Process_Time( int secs );
381 
382 int LSAPI_Init_Env_Parameters( fn_select_t fp );
383 
384 void LSAPI_Set_Slow_Req_Msecs( int msecs );
385 
386 int  LSAPI_Get_Slow_Req_Msecs(void);
387 
388 int LSAPI_is_suEXEC_Daemon(void);
389 
390 int LSAPI_Set_Restored_Parent_Pid(int pid);
391 
392 typedef void (*LSAPI_On_Timer_pf)(int *forked_child_pid);
393 void LSAPI_Register_Pgrp_Timer_Callback(LSAPI_On_Timer_pf);
394 
395 int LSAPI_Inc_Req_Processed(int cnt);
396 
397 int LSAPI_Accept_Before_Fork(LSAPI_Request * pReq);
398 
399 int LSAPI_Postfork_Child(LSAPI_Request * pReq);
400 
401 int LSAPI_Postfork_Parent(LSAPI_Request * pReq);
402 
403 #define LSAPI_LOG_LEVEL_BITS    0xff
404 #define LSAPI_LOG_FLAG_NONE     0
405 #define LSAPI_LOG_FLAG_DEBUG    1
406 #define LSAPI_LOG_FLAG_INFO     2
407 #define LSAPI_LOG_FLAG_NOTICE   3
408 #define LSAPI_LOG_FLAG_WARN     4
409 #define LSAPI_LOG_FLAG_ERROR    5
410 #define LSAPI_LOG_FLAG_CRIT     6
411 #define LSAPI_LOG_FLAG_FATAL    7
412 
413 #define LSAPI_LOG_TIMESTAMP_BITS (0xff00)
414 #define LSAPI_LOG_TIMESTAMP_FULL (0x100)
415 #define LSAPI_LOG_TIMESTAMP_HMS  (0x200)
416 #define LSAPI_LOG_TIMESTAMP_STDERR  (0x400)
417 
418 #define LSAPI_LOG_PID            (0x10000)
419 
420 void LSAPI_Log(int flag, const char * fmt, ...)
421 #ifdef __GNUC__
422         __attribute__((format(printf, 2, 3)))
423 #endif
424 ;
425 
426 
427 #if defined (c_plusplus) || defined (__cplusplus)
428 }
429 #endif
430 
431 
432 #endif
433