fileserver_example_dirs.pp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. (*
  2. This file is part of libmicrohttpd
  3. Copyright (C) 2007 Christian Grothoff (and other contributing authors)
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  15. *)
  16. (**
  17. * @file fileserver_example_dirs.pp (Original: fileserver_example_dirs.c)
  18. * @brief example for how to use libmicrohttpd to serve files (with directory support)
  19. * @author Christian Grothoff / Silvio Clécio
  20. *)
  21. program fileserver_example_dirs;
  22. {$mode objfpc}{$H+}
  23. uses
  24. sysutils, BaseUnix, cutils, libmicrohttpd;
  25. const
  26. PAGE: Pcchar = '<html><head><title>File not found</title></head><body>File not found</body></html>';
  27. function file_reader(cls: Pointer; pos: cuint64; buf: Pcchar;
  28. max: size_t): ssize_t; cdecl;
  29. var
  30. &file: FILEptr;
  31. begin
  32. &file := cls;
  33. fseek(&file, pos, SEEK_SET);
  34. Result := fread(buf, 1, max, &file);
  35. end;
  36. procedure file_free_callback(cls: Pointer); cdecl;
  37. var
  38. &file: FILEptr;
  39. begin
  40. &file := cls;
  41. fclose(&file);
  42. end;
  43. procedure dir_free_callback(cls: Pointer); cdecl;
  44. var
  45. dir: pDir;
  46. begin
  47. dir := cls;
  48. if dir <> nil then
  49. FpClosedir(dir^);
  50. end;
  51. function dir_reader(cls: Pointer; pos: cuint64; buf: Pcchar;
  52. max: size_t): ssize_t; cdecl;
  53. var
  54. dir: pDir;
  55. e: pDirent;
  56. begin
  57. dir := cls;
  58. if max < 512 then
  59. Exit(0);
  60. repeat
  61. e := FpReaddir(dir^);
  62. if e = nil then
  63. Exit(MHD_CONTENT_READER_END_OF_STREAM);
  64. until not (e^.d_name[0] = '.');
  65. Result := snprintf(buf, max, '<a href="/%s">%s</a><br>', e^.d_name,
  66. e^.d_name);
  67. end;
  68. function ahc_echo(cls: Pointer; connection: PMHD_Connection; url: Pcchar;
  69. method: Pcchar; version: Pcchar; upload_data: Pcchar;
  70. upload_data_size: Psize_t; ptr: PPointer): cint; cdecl;
  71. const
  72. aptr: cint = 0;
  73. var
  74. response: PMHD_Response;
  75. ret: cint;
  76. &file: FILEptr;
  77. dir: pDir;
  78. buf: stat;
  79. emsg: array[0..1023] of AnsiChar;
  80. begin
  81. if 0 <> strcomp(method, MHD_HTTP_METHOD_GET) then
  82. Exit(MHD_NO); (* unexpected method *)
  83. if @aptr <> ptr^ then
  84. begin
  85. (* do never respond on first call *)
  86. ptr^ := @aptr;
  87. Exit(MHD_YES);
  88. end;
  89. ptr^ := nil; (* reset when done *)
  90. if (0 = FpStat(@url[1], buf)) and fpS_ISREG(buf.st_mode) then
  91. &file := fopen(@url[1], fopenread)
  92. else
  93. &file := nil;
  94. if &file = nil then
  95. begin
  96. dir := FpOpendir(PChar('.'));
  97. if dir = nil then
  98. begin
  99. (* most likely cause: more concurrent requests than
  100. available file descriptors / 2 *)
  101. snprintf(emsg, SizeOf(emsg), 'Failed to open directory `.'': %s'#10,
  102. strerror(errno^));
  103. response := MHD_create_response_from_buffer(strlen(emsg), @emsg,
  104. MHD_RESPMEM_MUST_COPY);
  105. if response = nil then
  106. Exit(MHD_NO);
  107. ret := MHD_queue_response(connection, MHD_HTTP_SERVICE_UNAVAILABLE,
  108. response);
  109. MHD_destroy_response(response);
  110. end
  111. else
  112. begin
  113. response := MHD_create_response_from_callback(cuint64(MHD_SIZE_UNKNOWN),
  114. 32 * 1024, @dir_reader, dir, @dir_free_callback);
  115. if response = nil then
  116. begin
  117. FpClosedir(dir^);
  118. Exit(MHD_NO);
  119. end;
  120. ret := MHD_queue_response(connection, MHD_HTTP_OK, response);
  121. MHD_destroy_response(response);
  122. end;
  123. end
  124. else
  125. begin
  126. response := MHD_create_response_from_callback(buf.st_size, 32 * 1024, (* 32k page size *)
  127. @file_reader, &file, @file_free_callback);
  128. if response = nil then
  129. begin
  130. fclose(&file);
  131. Exit(MHD_NO);
  132. end;
  133. ret := MHD_queue_response(connection, MHD_HTTP_OK, response);
  134. MHD_destroy_response(response);
  135. end;
  136. Result := ret;
  137. end;
  138. var
  139. d: PMHD_Daemon;
  140. begin
  141. if argc <> 2 then
  142. begin
  143. WriteLn(argv[0], ' PORT');
  144. Halt(1);
  145. end;
  146. d := MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION or MHD_USE_DEBUG,
  147. StrToInt(argv[1]), nil, nil, @ahc_echo, PAGE, MHD_OPTION_END);
  148. if d = nil then
  149. Halt(1);
  150. ReadLn;
  151. MHD_stop_daemon(d);
  152. end.