1#!/usr/bin/env perl
2#***************************************************************************
3#                                  _   _ ____  _
4#  Project                     ___| | | |  _ \| |
5#                             / __| | | | |_) | |
6#                            | (__| |_| |  _ <| |___
7#                             \___|\___/|_| \_\_____|
8#
9# Copyright (C) 1998 - 2021, 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#***************************************************************************
23
24BEGIN {
25    push(@INC, $ENV{'srcdir'}) if(defined $ENV{'srcdir'});
26    push(@INC, ".");
27}
28
29use strict;
30use warnings;
31
32use serverhelp qw(
33    server_pidfilename
34    server_logfilename
35    );
36
37use sshhelp qw(
38    exe_ext
39    );
40
41my $verbose = 0;     # set to 1 for debugging
42my $port = 8990;     # just a default
43my $unix_socket;     # location to place a listening Unix socket
44my $ipvnum = 4;      # default IP version of http server
45my $idnum = 1;       # default http server instance number
46my $proto = 'http';  # protocol the http server speaks
47my $pidfile;         # pid file
48my $portfile;        # port number file
49my $logfile;         # log file
50my $connect;         # IP to connect to on CONNECT
51my $srcdir;
52my $gopher = 0;
53
54my $flags  = "";
55my $path   = '.';
56my $logdir = $path .'/log';
57
58while(@ARGV) {
59    if($ARGV[0] eq '--pidfile') {
60        if($ARGV[1]) {
61            $pidfile = $ARGV[1];
62            shift @ARGV;
63        }
64    }
65    elsif($ARGV[0] eq '--portfile') {
66        if($ARGV[1]) {
67            $portfile = $ARGV[1];
68            shift @ARGV;
69        }
70    }
71    elsif($ARGV[0] eq '--logfile') {
72        if($ARGV[1]) {
73            $logfile = $ARGV[1];
74            shift @ARGV;
75        }
76    }
77    elsif($ARGV[0] eq '--srcdir') {
78        if($ARGV[1]) {
79            $srcdir = $ARGV[1];
80            shift @ARGV;
81        }
82    }
83    elsif($ARGV[0] eq '--ipv4') {
84        $ipvnum = 4;
85    }
86    elsif($ARGV[0] eq '--ipv6') {
87        $ipvnum = 6;
88    }
89    elsif($ARGV[0] eq '--unix-socket') {
90        $ipvnum = 'unix';
91        if($ARGV[1]) {
92            $unix_socket = $ARGV[1];
93            shift @ARGV;
94        }
95    }
96    elsif($ARGV[0] eq '--gopher') {
97        $gopher = 1;
98    }
99    elsif($ARGV[0] eq '--port') {
100        if($ARGV[1] =~ /^(\d+)$/) {
101            $port = $1;
102            shift @ARGV;
103        }
104    }
105    elsif($ARGV[0] eq '--connect') {
106        if($ARGV[1]) {
107            $connect = $ARGV[1];
108            shift @ARGV;
109        }
110    }
111    elsif($ARGV[0] eq '--id') {
112        if($ARGV[1] =~ /^(\d+)$/) {
113            $idnum = $1 if($1 > 0);
114            shift @ARGV;
115        }
116    }
117    elsif($ARGV[0] eq '--verbose') {
118        $verbose = 1;
119    }
120    else {
121        print STDERR "\nWarning: httpserver.pl unknown parameter: $ARGV[0]\n";
122    }
123    shift @ARGV;
124}
125
126if(!$srcdir) {
127    $srcdir = $ENV{'srcdir'} || '.';
128}
129if(!$pidfile) {
130    $pidfile = "$path/". server_pidfilename($proto, $ipvnum, $idnum);
131}
132if(!$portfile) {
133    $portfile = "$path/". server_portfilename($proto, $ipvnum, $idnum);
134}
135if(!$logfile) {
136    $logfile = server_logfilename($logdir, $proto, $ipvnum, $idnum);
137}
138
139$flags .= "--pidfile \"$pidfile\" ".
140    "--logfile \"$logfile\" ".
141    "--portfile \"$portfile\" ";
142$flags .= "--gopher " if($gopher);
143$flags .= "--connect $connect " if($connect);
144if($ipvnum eq 'unix') {
145    $flags .= "--unix-socket '$unix_socket' ";
146} else {
147    $flags .= "--ipv$ipvnum --port $port ";
148}
149$flags .= "--srcdir \"$srcdir\"";
150
151if($verbose) {
152    print STDERR "RUN: server/sws".exe_ext('SRV')." $flags\n";
153}
154
155$| = 1;
156exec("exec server/sws".exe_ext('SRV')." $flags");
157