2
0

http-server.pl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/usr/bin/env perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  10. #
  11. # This software is licensed as described in the file COPYING, which
  12. # you should have received as part of this distribution. The terms
  13. # are also available at https://curl.se/docs/copyright.html.
  14. #
  15. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. # copies of the Software, and permit persons to whom the Software is
  17. # furnished to do so, under the terms of the COPYING file.
  18. #
  19. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. # KIND, either express or implied.
  21. #
  22. # SPDX-License-Identifier: curl
  23. #
  24. #***************************************************************************
  25. use strict;
  26. use warnings;
  27. BEGIN {
  28. push(@INC, $ENV{'srcdir'}) if(defined $ENV{'srcdir'});
  29. push(@INC, ".");
  30. }
  31. use File::Basename;
  32. use serverhelp qw(
  33. server_pidfilename
  34. server_logfilename
  35. );
  36. use pathhelp qw(
  37. exe_ext
  38. );
  39. my $verbose = 0; # set to 1 for debugging
  40. my $port = 8990; # just a default
  41. my $unix_socket; # location to place a listening Unix socket
  42. my $ipvnum = 4; # default IP version of http server
  43. my $idnum = 1; # default http server instance number
  44. my $proto = 'http'; # protocol the http server speaks
  45. my $pidfile; # pid file
  46. my $portfile; # port number file
  47. my $logfile; # log file
  48. my $cmdfile; # command file
  49. my $connect; # IP to connect to on CONNECT
  50. my $keepalive_secs; # number of seconds to keep idle connections
  51. my $srcdir;
  52. my $gopher = 0;
  53. my $flags = "";
  54. my $path = '.';
  55. my $logdir = $path .'/log';
  56. my $piddir;
  57. while(@ARGV) {
  58. if($ARGV[0] eq '--pidfile') {
  59. if($ARGV[1]) {
  60. $pidfile = $ARGV[1];
  61. shift @ARGV;
  62. }
  63. }
  64. elsif($ARGV[0] eq '--portfile') {
  65. if($ARGV[1]) {
  66. $portfile = $ARGV[1];
  67. shift @ARGV;
  68. }
  69. }
  70. elsif($ARGV[0] eq '--config') {
  71. if($ARGV[1]) {
  72. $cmdfile = $ARGV[1];
  73. shift @ARGV;
  74. }
  75. }
  76. elsif($ARGV[0] eq '--logfile') {
  77. if($ARGV[1]) {
  78. $logfile = $ARGV[1];
  79. shift @ARGV;
  80. }
  81. }
  82. elsif($ARGV[0] eq '--logdir') {
  83. if($ARGV[1]) {
  84. $logdir = $ARGV[1];
  85. shift @ARGV;
  86. }
  87. }
  88. elsif($ARGV[0] eq '--srcdir') {
  89. if($ARGV[1]) {
  90. $srcdir = $ARGV[1];
  91. shift @ARGV;
  92. }
  93. }
  94. elsif($ARGV[0] eq '--ipv4') {
  95. $ipvnum = 4;
  96. }
  97. elsif($ARGV[0] eq '--ipv6') {
  98. $ipvnum = 6;
  99. }
  100. elsif($ARGV[0] eq '--unix-socket') {
  101. $ipvnum = 'unix';
  102. if($ARGV[1]) {
  103. $unix_socket = $ARGV[1];
  104. shift @ARGV;
  105. }
  106. }
  107. elsif($ARGV[0] eq '--gopher') {
  108. $gopher = 1;
  109. }
  110. elsif($ARGV[0] eq '--port') {
  111. if($ARGV[1] =~ /^(\d+)$/) {
  112. $port = $1;
  113. shift @ARGV;
  114. }
  115. }
  116. elsif($ARGV[0] eq '--connect') {
  117. if($ARGV[1]) {
  118. $connect = $ARGV[1];
  119. shift @ARGV;
  120. }
  121. }
  122. elsif($ARGV[0] eq '--keepalive') {
  123. if($ARGV[1]) {
  124. $keepalive_secs = $ARGV[1];
  125. shift @ARGV;
  126. }
  127. }
  128. elsif($ARGV[0] eq '--id') {
  129. if($ARGV[1] =~ /^(\d+)$/) {
  130. $idnum = $1 if($1 > 0);
  131. shift @ARGV;
  132. }
  133. }
  134. elsif($ARGV[0] eq '--verbose') {
  135. $verbose = 1;
  136. }
  137. else {
  138. print STDERR "\nWarning: http-server.pl unknown parameter: $ARGV[0]\n";
  139. }
  140. shift @ARGV;
  141. }
  142. #***************************************************************************
  143. # Initialize command line option dependent variables
  144. #
  145. if($pidfile) {
  146. # Use our pidfile directory to store the other pidfiles
  147. $piddir = dirname($pidfile);
  148. }
  149. else {
  150. # Use the current directory to store all the pidfiles
  151. $piddir = $path;
  152. $pidfile = server_pidfilename($piddir, $proto, $ipvnum, $idnum);
  153. }
  154. if(!$portfile) {
  155. $portfile = server_portfilename($piddir, $proto, $ipvnum, $idnum);
  156. }
  157. if(!$srcdir) {
  158. $srcdir = $ENV{'srcdir'} || '.';
  159. }
  160. if(!$logfile) {
  161. $logfile = server_logfilename($logdir, $proto, $ipvnum, $idnum);
  162. }
  163. $flags .= "--pidfile \"$pidfile\" ".
  164. "--cmdfile \"$cmdfile\" ".
  165. "--logfile \"$logfile\" ".
  166. "--logdir \"$logdir\" ".
  167. "--portfile \"$portfile\" ";
  168. $flags .= "--gopher " if($gopher);
  169. $flags .= "--connect $connect " if($connect);
  170. $flags .= "--keepalive $keepalive_secs " if($keepalive_secs);
  171. if($ipvnum eq 'unix') {
  172. $flags .= "--unix-socket '$unix_socket' ";
  173. } else {
  174. $flags .= "--ipv$ipvnum --port $port ";
  175. }
  176. $flags .= "--srcdir \"$srcdir\"";
  177. if($verbose) {
  178. print STDERR "RUN: server/sws".exe_ext('SRV')." $flags\n";
  179. }
  180. $| = 1;
  181. exec("exec server/sws".exe_ext('SRV')." $flags");