httpd24.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. {
  2. httpd.pas
  3. Copyright (C) 2006 Felipe Monteiro de Carvalho
  4. (based on the Apache 2.0.58 headers)
  5. Updated by Attila Borka in 2012 for the Apache 2.4.3 headers
  6. This unit is a pascal binding for the Apache 2.4.3 headers.
  7. The headers were released under the following copyright:
  8. }
  9. { Licensed to the Apache Software Foundation (ASF) under one or more
  10. * contributor license agreements. See the NOTICE file distributed with
  11. * this work for additional information regarding copyright ownership.
  12. * The ASF licenses this file to You under the Apache License, Version 2.0
  13. * (the "License"); you may not use this file except in compliance with
  14. * the License. You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. }
  24. {*
  25. * @file httpd.h
  26. * @brief HTTP Daemon routines
  27. *
  28. * @defgroup APACHE Apache HTTP Server
  29. *
  30. * Top level group of which all other groups are a member
  31. * @
  32. *
  33. * @defgroup APACHE_MODS Loadable modules
  34. * Top level group for modules
  35. * @defgroup APACHE_OS Operating System Specific
  36. * @defgroup APACHE_INTERNAL Internal interfaces
  37. * @defgroup APACHE_CORE Core routines
  38. * @
  39. * @defgroup APACHE_CORE_DAEMON HTTP Daemon Routine
  40. * @
  41. }
  42. {$IFNDEF FPC_DOTTEDUNITS}
  43. unit httpd24;
  44. {$ENDIF FPC_DOTTEDUNITS}
  45. {$ifdef fpc}
  46. {$mode delphi}
  47. {$modeswitch unicodestrings-}
  48. {$H-}
  49. {$endif}
  50. {$ifdef Unix}
  51. {$PACKRECORDS C}
  52. {$endif}
  53. {$PACKENUM 4}
  54. {$IFDEF Apache1_3}
  55. {$WARNING Apache1_3 is defined somewhere, but the HTTPD unit included is for Apache2_4}
  56. {$ENDIF}
  57. {$IFDEF Apache2_0}
  58. {$WARNING Apache2_0 is defined somewhere, but the HTTPD unit included is for Apache2_4}
  59. {$ENDIF}
  60. {$IFDEF Apache2_2}
  61. {$WARNING Apache2_2 is defined somewhere, but the HTTPD unit included is for Apache2_4}
  62. {$ENDIF}
  63. {$IFDEF FPCAPACHE_1_3}
  64. {$WARNING FPCAPACHE_1_3 is defined somewhere, but the HTTPD unit included is for FPCAPACHE_2_4}
  65. {$ENDIF}
  66. {$IFDEF FPCAPACHE_2_0}
  67. {$WARNING FPCAPACHE_2_0 is defined somewhere, but the HTTPD unit included is for FPCAPACHE_2_4}
  68. {$ENDIF}
  69. {$IFDEF FPCAPACHE_2_2}
  70. {$WARNING FPCAPACHE_2_2 is defined somewhere, but the HTTPD unit included is for FPCAPACHE_2_4}
  71. {$ENDIF}
  72. {$DEFINE Apache2_4}
  73. {$DEFINE FPCAPACHE_2_4}
  74. interface
  75. {$IFDEF FPC_DOTTEDUNITS}
  76. uses
  77. {$ifdef Windows}
  78. WinApi.Windows,
  79. {$ELSE}
  80. UnixApi.Types,
  81. {$ENDIF}
  82. System.CTypes, Api.HttpD24.Apr;
  83. {$ELSE FPC_DOTTEDUNITS}
  84. uses
  85. {$ifdef WINDOWS}
  86. Windows,
  87. {$ELSE}
  88. UnixType,
  89. {$ENDIF}
  90. ctypes, apr24;
  91. {$ENDIF FPC_DOTTEDUNITS}
  92. const
  93. {$ifndef fpc}
  94. LineEnding = #13#10;
  95. {$endif}
  96. {$IFDEF WINDOWS}
  97. LibHTTPD = 'libhttpd.dll';
  98. {$ELSE}
  99. LibHTTPD = '';
  100. {$ENDIF}
  101. {$IFDEF WINDOWS}
  102. LibAPRUtil = 'libaprutil-1.dll';
  103. {$ELSE}
  104. LibAPRUtil = '';
  105. {$ENDIF}
  106. type
  107. { configuration vector structure , moved from http_config.inc (http_config.h)}
  108. ap_conf_vector_t = record end;
  109. Pap_conf_vector_t = ^ap_conf_vector_t;
  110. PPap_conf_vector_t = ^Pap_conf_vector_t;
  111. {*
  112. Shortcuts for FPC, so no extra includes are needed.
  113. It would require more of the header files from the Apache httpd, apr and apr-util
  114. source code packages.
  115. *}
  116. {apr_thread_mutex_t is OS dependent, found in apr-X.X.X/include/arch/.../apr_arch_thread_mutex.h}
  117. Papr_thread_mutex_t = Pointer;//^apr_thread_mutex_t; used in http.inc -> request_rec record
  118. {from apr-X.X.X/include/apr_network_io.h used in server_addr_rec record in httpd.inc}
  119. Papr_sockaddr_t = Pointer;//^apr_sockaddr_t
  120. apr_port_t = word;//apr_uint16_t
  121. {end apr_network_io.h}
  122. { A structure to represent sockets }
  123. apr_socket_t = record end;
  124. Papr_socket_t = ^apr_socket_t;
  125. PPapr_socket_t = ^Papr_socket_t;
  126. {end apr_network_io.h}
  127. {from apr-X.X.X/include/apr_thread_proc.h , used in http_log.h (http_log.inc)}
  128. apr_cmdtype_e = (
  129. APR_SHELLCMD, //**< use the shell to invoke the program */
  130. APR_PROGRAM, //**< invoke the program directly, no copied env */
  131. APR_PROGRAM_ENV, //**< invoke the program, replicating our environment */
  132. APR_PROGRAM_PATH, //**< find program on PATH, use our environment */
  133. APR_SHELLCMD_ENV {/**< use the shell to invoke the program,
  134. * replicating our environment
  135. *}
  136. );
  137. {*
  138. end Shortcuts for FPC
  139. *}
  140. {
  141. Main httpd header files
  142. Note: There are more include files other then these, because some include files
  143. include more files.
  144. }
  145. //{$include ap_provider.inc}
  146. {$include util_cfgtree.inc}
  147. {$include httpd.inc}
  148. {$include http_config.inc}
  149. {$include http_core.inc}
  150. {$include http_log.inc}
  151. //{$include http_main.inc}
  152. {$include http_protocol.inc}
  153. //{$include http_request.inc}
  154. //{$include http_connection.inc}
  155. //{$include http_vhost.inc}
  156. {$include util_script.inc}
  157. //{$include util_time.inc}
  158. //{$include util_md5.inc}
  159. //{$include ap_mpm.inc}
  160. // APRUtil External Variables //
  161. var
  162. {/* All of the bucket types implemented by the core */
  163. /**
  164. * The flush bucket type. This signifies that all data should be flushed to
  165. * the next filter. The flush bucket should be sent with the other buckets.
  166. */}
  167. apr_bucket_type_flush: apr_bucket_type_t external LibAPRUtil;
  168. {/**
  169. * The EOS bucket type. This signifies that there will be no more data, ever.
  170. * All filters MUST send all data to the next filter when they receive a
  171. * bucket of this type
  172. */}
  173. apr_bucket_type_eos: apr_bucket_type_t external LibAPRUtil;
  174. {/**
  175. * The FILE bucket type. This bucket represents a file on disk
  176. */}
  177. apr_bucket_type_file: apr_bucket_type_t external LibAPRUtil;
  178. {/**
  179. * The HEAP bucket type. This bucket represents a data allocated from the
  180. * heap.
  181. */}
  182. apr_bucket_type_heap: apr_bucket_type_t external LibAPRUtil;
  183. {$IFDEF APR_HAS_MMAP}
  184. {/**
  185. * The MMAP bucket type. This bucket represents an MMAP'ed file
  186. */}
  187. apr_bucket_type_mmap: apr_bucket_type_t external LibAPRUtil;
  188. {$ENDIF}
  189. {/**
  190. * The POOL bucket type. This bucket represents a data that was allocated
  191. * from a pool. IF this bucket is still available when the pool is cleared,
  192. * the data is copied on to the heap.
  193. */}
  194. apr_bucket_type_pool: apr_bucket_type_t external LibAPRUtil;
  195. {/**
  196. * The PIPE bucket type. This bucket represents a pipe to another program.
  197. */}
  198. apr_bucket_type_pipe: apr_bucket_type_t external LibAPRUtil;
  199. {/**
  200. * The IMMORTAL bucket type. This bucket represents a segment of data that
  201. * the creator is willing to take responsibility for. The core will do
  202. * nothing with the data in an immortal bucket
  203. */}
  204. apr_bucket_type_immortal: apr_bucket_type_t external LibAPRUtil;
  205. {/**
  206. * The TRANSIENT bucket type. This bucket represents a data allocated off
  207. * the stack. When the setaside function is called, this data is copied on
  208. * to the heap
  209. */}
  210. apr_bucket_type_transient: apr_bucket_type_t external LibAPRUtil;
  211. {/**
  212. * The SOCKET bucket type. This bucket represents a socket to another machine
  213. */}
  214. apr_bucket_type_socket: apr_bucket_type_t external LibAPRUtil;
  215. //********************************************************************
  216. { from apr_buckets.inc }
  217. function APR_BRIGADE_SENTINEL(b: Papr_bucket_brigade): Papr_bucket;
  218. function APR_BRIGADE_FIRST(b: Papr_bucket_brigade): Papr_bucket;
  219. function APR_BRIGADE_LAST(b: Papr_bucket_brigade): Papr_bucket;
  220. function APR_BUCKET_NEXT(e: Papr_bucket): Papr_bucket;
  221. function APR_BUCKET_PREV(e: Papr_bucket): Papr_bucket;
  222. procedure APR_BUCKET_REMOVE(e: Papr_bucket);
  223. function APR_BUCKET_IS_METADATA(e: Papr_bucket): boolean;
  224. function APR_BUCKET_IS_FLUSH(e: Papr_bucket): boolean;
  225. function APR_BUCKET_IS_EOS(e: Papr_bucket): boolean;
  226. function APR_BUCKET_IS_FILE(e: Papr_bucket): boolean;
  227. function APR_BUCKET_IS_PIPE(e: Papr_bucket): boolean;
  228. function APR_BUCKET_IS_SOCKET(e: Papr_bucket): boolean;
  229. function APR_BUCKET_IS_HEAP(e: Papr_bucket): boolean;
  230. function APR_BUCKET_IS_TRANSIENT(e: Papr_bucket): boolean;
  231. function APR_BUCKET_IS_IMMORTAL(e: Papr_bucket): boolean;
  232. {$IFDEF APR_HAS_MMAP}
  233. function APR_BUCKET_IS_MMAP(e: Papr_bucket): boolean;
  234. {$ENDIF}
  235. function APR_BUCKET_IS_POOL(e: Papr_bucket): boolean;
  236. function apr_bucket_read(e: Papr_bucket; const str: PPAnsiChar; len: Papr_size_t;
  237. block: apr_read_type_e): apr_status_t;
  238. function AP_INIT_TAKE1(directive: PAnsiChar; const take1func : ttake1func;
  239. mconfig: Pointer; where: Integer; help: PAnsiChar): command_rec;
  240. function AP_INIT_TAKE2(directive: PAnsiChar; const take2func: ttake2func;
  241. mconfig: Pointer; where: Integer; help: PAnsiChar): command_rec;
  242. function AP_INIT_TAKE3(directive: PAnsiChar; const take3func: ttake3func;
  243. mconfig: Pointer; where: Integer; help: PAnsiChar): command_rec;
  244. implementation
  245. { Internal representation for a HTTP protocol number, e.g., HTTP/1.1 }
  246. function HTTP_VERSION(major, minor: Integer): Integer;
  247. begin
  248. HTTP_VERSION := (1000 * major + minor);
  249. end;
  250. { Major part of HTTP protocol }
  251. function HTTP_VERSION_MAJOR(number: Integer): Integer;
  252. begin
  253. HTTP_VERSION_MAJOR := number div 1000;
  254. end;
  255. { Minor part of HTTP protocol }
  256. function HTTP_VERSION_MINOR(number: Integer): Integer;
  257. begin
  258. HTTP_VERSION_MINOR := number mod 1000;
  259. end;
  260. function ap_is_HTTP_INFO(x : Integer): Boolean;
  261. begin
  262. ap_is_HTTP_INFO := ((x>=100) and (x<200));
  263. end;
  264. function ap_is_HTTP_SUCCESS(x : Integer) : Boolean;
  265. begin
  266. ap_is_HTTP_SUCCESS := ((x>=200) and (x<300));
  267. end;
  268. function ap_is_HTTP_REDIRECT(x : Integer) : Boolean;
  269. begin
  270. ap_is_HTTP_REDIRECT := ((x>=300) and (x<400));
  271. end;
  272. function ap_is_HTTP_ERROR(x : Integer) : Boolean;
  273. begin
  274. ap_is_HTTP_ERROR := ((x>=400) and (x<600));
  275. end;
  276. function ap_is_HTTP_CLIENT_ERROR(x : Integer) : Boolean;
  277. begin
  278. ap_is_HTTP_CLIENT_ERROR := ((x>=400) and (x<500));
  279. end;
  280. function ap_is_HTTP_SERVER_ERROR(x : Integer) : Boolean;
  281. begin
  282. ap_is_HTTP_SERVER_ERROR := ((x>=500) and (x<600));
  283. end;
  284. function ap_is_HTTP_VALID_RESPONSE(x : Integer) : Boolean;
  285. begin
  286. ap_is_HTTP_VALID_RESPONSE := ((x>=100) and (x<600));
  287. end;
  288. function ap_status_drops_connection(x : Integer): Boolean;
  289. begin
  290. case x of
  291. HTTP_BAD_REQUEST,
  292. HTTP_REQUEST_TIME_OUT,
  293. HTTP_LENGTH_REQUIRED,
  294. HTTP_REQUEST_ENTITY_TOO_LARGE,
  295. HTTP_REQUEST_URI_TOO_LARGE,
  296. HTTP_INTERNAL_SERVER_ERROR,
  297. HTTP_SERVICE_UNAVAILABLE,
  298. HTTP_NOT_IMPLEMENTED:
  299. Result := true;
  300. else
  301. Result := false;
  302. end;
  303. end;
  304. function ap_escape_uri(ppool: Papr_pool_t; const path: PAnsiChar) : PAnsiChar;
  305. begin
  306. ap_escape_uri:=ap_os_escape_path(ppool,path,1);
  307. end;
  308. function ap_escape_html(p: Papr_pool_t; const s: PAnsiChar) : PAnsiChar;
  309. begin
  310. ap_escape_html:=ap_escape_html2(p,s,0);
  311. end;
  312. //********************************************************************
  313. { from apr_buckets.inc }
  314. function APR_BRIGADE_FIRST(b: Papr_bucket_brigade): Papr_bucket; inline;
  315. begin
  316. APR_BRIGADE_FIRST := b^.list.next;
  317. end;
  318. function APR_BRIGADE_LAST(b: Papr_bucket_brigade): Papr_bucket; inline;
  319. begin
  320. APR_BRIGADE_LAST := b^.list.prev;
  321. end;
  322. function APR_BRIGADE_SENTINEL(b: Papr_bucket_brigade): Papr_bucket; inline;
  323. var b_: apr_bucket; // This should technically be <type> and link shouldn't be hard-coded..
  324. begin
  325. APR_BRIGADE_SENTINEL := Papr_bucket(pointer(@b^.list.next) - (pointer(@b_.Link) - pointer(@b_) ) );
  326. end;
  327. function APR_BUCKET_IS_METADATA(e: Papr_bucket): boolean; inline;
  328. begin
  329. APR_BUCKET_IS_METADATA := e^.type_^.is_metadata = APR_BUCKET_METADATA;
  330. end;
  331. function APR_BUCKET_IS_FLUSH(e: Papr_bucket): boolean; inline;
  332. begin
  333. APR_BUCKET_IS_FLUSH := e^.type_ = @apr_bucket_type_flush;
  334. end;
  335. function APR_BUCKET_IS_EOS(e: Papr_bucket): boolean; inline;
  336. begin
  337. APR_BUCKET_IS_EOS := e^.type_ = @apr_bucket_type_eos;
  338. end;
  339. function APR_BUCKET_IS_FILE(e: Papr_bucket): boolean; inline;
  340. begin
  341. APR_BUCKET_IS_FILE := e^.type_ = @apr_bucket_type_file;
  342. end;
  343. function APR_BUCKET_IS_PIPE(e: Papr_bucket): boolean; inline;
  344. begin
  345. APR_BUCKET_IS_PIPE := e^.type_ = @apr_bucket_type_pipe;
  346. end;
  347. function APR_BUCKET_IS_SOCKET(e: Papr_bucket): boolean; inline;
  348. begin
  349. APR_BUCKET_IS_SOCKET := e^.type_ = @apr_bucket_type_socket;
  350. end;
  351. function APR_BUCKET_IS_HEAP(e: Papr_bucket): boolean; inline;
  352. begin
  353. APR_BUCKET_IS_HEAP := e^.type_ = @apr_bucket_type_heap;
  354. end;
  355. function APR_BUCKET_IS_TRANSIENT(e: Papr_bucket): boolean; inline;
  356. begin
  357. APR_BUCKET_IS_TRANSIENT := e^.type_ = @apr_bucket_type_transient;
  358. end;
  359. function APR_BUCKET_IS_IMMORTAL(e: Papr_bucket): boolean; inline;
  360. begin
  361. APR_BUCKET_IS_IMMORTAL := e^.type_ = @apr_bucket_type_immortal;
  362. end;
  363. {$IFDEF APR_HAS_MMAP}
  364. function APR_BUCKET_IS_MMAP(e: Papr_bucket): boolean; inline;
  365. begin
  366. APR_BUCKET_IS_MMAP := e^.type_ = @apr_bucket_type_mmap;
  367. end;
  368. {$ENDIF}
  369. function APR_BUCKET_IS_POOL(e: Papr_bucket): boolean; inline;
  370. begin
  371. APR_BUCKET_IS_POOL := e^.type_ = @apr_bucket_type_pool;
  372. end;
  373. function APR_BUCKET_NEXT(e: Papr_bucket): Papr_bucket; inline;
  374. begin
  375. APR_BUCKET_NEXT := e^.link.next;
  376. end;
  377. function APR_BUCKET_PREV(e: Papr_bucket): Papr_bucket; inline;
  378. begin
  379. APR_BUCKET_PREV := e^.link.prev;
  380. end;
  381. procedure APR_BUCKET_REMOVE(e: Papr_bucket); inline;
  382. begin
  383. APR_BUCKET_PREV(e)^.link.next := APR_BUCKET_NEXT(e);
  384. APR_BUCKET_NEXT(e)^.link.prev := APR_BUCKET_PREV(e);
  385. end;
  386. function apr_bucket_read(e: Papr_bucket; const str: PPAnsiChar; len: Papr_size_t;
  387. block: apr_read_type_e): apr_status_t; inline;
  388. begin
  389. apr_bucket_read := e^.type_^.read(e, str, len, block);
  390. end;
  391. function AP_INIT_TAKE1(directive: PAnsiChar; const take1func: ttake1func;
  392. mconfig: Pointer; where: Integer; help: PAnsiChar): command_rec; inline;
  393. begin
  394. with result DO
  395. begin
  396. name := directive;
  397. func.take1 := take1func;
  398. cmd_data := mconfig;
  399. req_override := where;
  400. args_how := TAKE1;
  401. errmsg := help;
  402. end;
  403. end;
  404. function AP_INIT_TAKE2(directive: PAnsiChar; const take2func: ttake2func;
  405. mconfig: Pointer; where: Integer; help: PAnsiChar): command_rec; inline;
  406. begin
  407. with result DO
  408. begin
  409. name := directive;
  410. func.take2 := take2func;
  411. cmd_data := mconfig;
  412. req_override := where;
  413. args_how := TAKE2;
  414. errmsg := help;
  415. end;
  416. end;
  417. function AP_INIT_TAKE3(directive: PAnsiChar; const take3func: ttake3func;
  418. mconfig: Pointer; where: Integer; help: PAnsiChar): command_rec; inline;
  419. begin
  420. with result DO
  421. begin
  422. name := directive;
  423. func.take3 := take3func;
  424. cmd_data := mconfig;
  425. req_override := where;
  426. args_how := TAKE3;
  427. errmsg := help;
  428. end;
  429. end;
  430. //********************************************************************
  431. { from http_config.inc }
  432. { Use this in all standard modules }
  433. procedure STANDARD20_MODULE_STUFF(var mod_: module);
  434. begin
  435. mod_.version := MODULE_MAGIC_NUMBER_MAJOR;
  436. mod_.minor_version := MODULE_MAGIC_NUMBER_MINOR;
  437. mod_.module_index := -1;
  438. // mod_.name: PAnsiChar;
  439. mod_.dynamic_load_handle := nil;
  440. mod_.next := nil;
  441. mod_.magic := MODULE_MAGIC_COOKIE;
  442. mod_.rewrite_args := nil;
  443. end;
  444. { Use this only in MPMs }
  445. procedure MPM20_MODULE_STUFF(var mod_: module);
  446. begin
  447. mod_.version := MODULE_MAGIC_NUMBER_MAJOR;
  448. mod_.minor_version := MODULE_MAGIC_NUMBER_MINOR;
  449. mod_.module_index := -1;
  450. // mod_.name: PAnsiChar;
  451. mod_.dynamic_load_handle := nil;
  452. mod_.next := nil;
  453. mod_.magic := MODULE_MAGIC_COOKIE;
  454. end;
  455. end.