xref: /curl/tests/http-server.pl (revision 200c4090)
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
26use strict;
27use warnings;
28
29BEGIN {
30    push(@INC, $ENV{'srcdir'}) if(defined $ENV{'srcdir'});
31    push(@INC, ".");
32}
33
34use File::Basename;
35
36use serverhelp qw(
37    server_pidfilename
38    server_logfilename
39    );
40
41use pathhelp qw(
42    exe_ext
43    );
44
45my $verbose = 0;     # set to 1 for debugging
46my $port = 8990;     # just a default
47my $unix_socket;     # location to place a listening Unix socket
48my $ipvnum = 4;      # default IP version of http server
49my $idnum = 1;       # default http server instance number
50my $proto = 'http';  # protocol the http server speaks
51my $pidfile;         # pid file
52my $portfile;        # port number file
53my $logfile;         # log file
54my $cmdfile;         # command file
55my $connect;         # IP to connect to on CONNECT
56my $keepalive_secs;  # number of seconds to keep idle connections
57my $srcdir;
58my $gopher = 0;
59
60my $flags  = "";
61my $path   = '.';
62my $logdir = $path .'/log';
63my $piddir;
64
65while(@ARGV) {
66    if($ARGV[0] eq '--pidfile') {
67        if($ARGV[1]) {
68            $pidfile = $ARGV[1];
69            shift @ARGV;
70        }
71    }
72    elsif($ARGV[0] eq '--portfile') {
73        if($ARGV[1]) {
74            $portfile = $ARGV[1];
75            shift @ARGV;
76        }
77    }
78    elsif($ARGV[0] eq '--config') {
79        if($ARGV[1]) {
80            $cmdfile = $ARGV[1];
81            shift @ARGV;
82        }
83    }
84    elsif($ARGV[0] eq '--logfile') {
85        if($ARGV[1]) {
86            $logfile = $ARGV[1];
87            shift @ARGV;
88        }
89    }
90    elsif($ARGV[0] eq '--logdir') {
91        if($ARGV[1]) {
92            $logdir = $ARGV[1];
93            shift @ARGV;
94        }
95    }
96    elsif($ARGV[0] eq '--srcdir') {
97        if($ARGV[1]) {
98            $srcdir = $ARGV[1];
99            shift @ARGV;
100        }
101    }
102    elsif($ARGV[0] eq '--ipv4') {
103        $ipvnum = 4;
104    }
105    elsif($ARGV[0] eq '--ipv6') {
106        $ipvnum = 6;
107    }
108    elsif($ARGV[0] eq '--unix-socket') {
109        $ipvnum = 'unix';
110        if($ARGV[1]) {
111            $unix_socket = $ARGV[1];
112            shift @ARGV;
113        }
114    }
115    elsif($ARGV[0] eq '--gopher') {
116        $gopher = 1;
117    }
118    elsif($ARGV[0] eq '--port') {
119        if($ARGV[1] =~ /^(\d+)$/) {
120            $port = $1;
121            shift @ARGV;
122        }
123    }
124    elsif($ARGV[0] eq '--connect') {
125        if($ARGV[1]) {
126            $connect = $ARGV[1];
127            shift @ARGV;
128        }
129    }
130    elsif($ARGV[0] eq '--keepalive') {
131        if($ARGV[1]) {
132            $keepalive_secs = $ARGV[1];
133            shift @ARGV;
134        }
135    }
136    elsif($ARGV[0] eq '--id') {
137        if($ARGV[1] =~ /^(\d+)$/) {
138            $idnum = $1 if($1 > 0);
139            shift @ARGV;
140        }
141    }
142    elsif($ARGV[0] eq '--verbose') {
143        $verbose = 1;
144    }
145    else {
146        print STDERR "\nWarning: http-server.pl unknown parameter: $ARGV[0]\n";
147    }
148    shift @ARGV;
149}
150
151#***************************************************************************
152# Initialize command line option dependent variables
153#
154
155if($pidfile) {
156    # Use our pidfile directory to store the other pidfiles
157    $piddir = dirname($pidfile);
158}
159else {
160    # Use the current directory to store all the pidfiles
161    $piddir = $path;
162    $pidfile = server_pidfilename($piddir, $proto, $ipvnum, $idnum);
163}
164if(!$portfile) {
165    $portfile = server_portfilename($piddir, $proto, $ipvnum, $idnum);
166}
167if(!$srcdir) {
168    $srcdir = $ENV{'srcdir'} || '.';
169}
170if(!$logfile) {
171    $logfile = server_logfilename($logdir, $proto, $ipvnum, $idnum);
172}
173
174$flags .= "--pidfile \"$pidfile\" ".
175    "--cmdfile \"$cmdfile\" ".
176    "--logfile \"$logfile\" ".
177    "--logdir \"$logdir\" ".
178    "--portfile \"$portfile\" ";
179$flags .= "--gopher " if($gopher);
180$flags .= "--connect $connect " if($connect);
181$flags .= "--keepalive $keepalive_secs " if($keepalive_secs);
182if($ipvnum eq 'unix') {
183    $flags .= "--unix-socket '$unix_socket' ";
184} else {
185    $flags .= "--ipv$ipvnum --port $port ";
186}
187$flags .= "--srcdir \"$srcdir\"";
188
189if($verbose) {
190    print STDERR "RUN: server/sws".exe_ext('SRV')." $flags\n";
191}
192
193$| = 1;
194exec("exec server/sws".exe_ext('SRV')." $flags");
195