httpd24.pas 15 KB

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