http2-server.pl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. # This script invokes nghttpx properly to have it serve HTTP/2 for us.
  26. # nghttpx runs as a proxy in front of our "actual" HTTP/1 server.
  27. use Cwd;
  28. use Cwd 'abs_path';
  29. use File::Basename;
  30. my $logdir = "log";
  31. my $pidfile = "$logdir/nghttpx.pid";
  32. my $logfile = "$logdir/http2.log";
  33. my $nghttpx = "nghttpx";
  34. my $listenport = 9015;
  35. my $listenport2 = 9016;
  36. my $connect = "127.0.0.1,8990";
  37. my $conf = "nghttpx.conf";
  38. my $cert = "Server-localhost-sv";
  39. my $dev_null = ($^O eq 'MSWin32' ? 'NUL' : '/dev/null');
  40. #***************************************************************************
  41. # Process command line options
  42. #
  43. while(@ARGV) {
  44. if($ARGV[0] eq '--verbose') {
  45. $verbose = 1;
  46. }
  47. elsif($ARGV[0] eq '--pidfile') {
  48. if($ARGV[1]) {
  49. $pidfile = $ARGV[1];
  50. shift @ARGV;
  51. }
  52. }
  53. elsif($ARGV[0] eq '--nghttpx') {
  54. if($ARGV[1]) {
  55. $nghttpx = $ARGV[1];
  56. shift @ARGV;
  57. }
  58. }
  59. elsif($ARGV[0] eq '--port') {
  60. if($ARGV[1]) {
  61. $listenport = $ARGV[1];
  62. shift @ARGV;
  63. }
  64. }
  65. elsif($ARGV[0] eq '--port2') {
  66. if($ARGV[1]) {
  67. $listenport2 = $ARGV[1];
  68. shift @ARGV;
  69. }
  70. }
  71. elsif($ARGV[0] eq '--connect') {
  72. if($ARGV[1]) {
  73. $connect = $ARGV[1];
  74. $connect =~ s/:/,/;
  75. shift @ARGV;
  76. }
  77. }
  78. elsif($ARGV[0] eq '--logfile') {
  79. if($ARGV[1]) {
  80. $logfile = $ARGV[1];
  81. shift @ARGV;
  82. }
  83. }
  84. elsif($ARGV[0] eq '--logdir') {
  85. if($ARGV[1]) {
  86. $logdir = $ARGV[1];
  87. shift @ARGV;
  88. }
  89. }
  90. elsif($ARGV[0] eq '--conf') {
  91. if($ARGV[1]) {
  92. $conf = $ARGV[1];
  93. shift @ARGV;
  94. }
  95. }
  96. else {
  97. print STDERR "\nWarning: http2-server.pl unknown parameter: $ARGV[0]\n";
  98. }
  99. shift @ARGV;
  100. }
  101. my $srcdir = dirname(__FILE__);
  102. $certfile = "$srcdir/certs/$cert.pem";
  103. $keyfile = "$srcdir/certs/$cert.key";
  104. $certfile = abs_path($certfile);
  105. $keyfile = abs_path($keyfile);
  106. my $cmdline="$nghttpx --backend=$connect ".
  107. "--backend-keep-alive-timeout=500ms ".
  108. "--frontend=\"*,$listenport;no-tls\" ".
  109. "--frontend=\"*,$listenport2\" ".
  110. "--log-level=INFO ".
  111. "--pid-file=$pidfile ".
  112. "--conf=$conf ".
  113. "--errorlog-file=$logfile ".
  114. "$keyfile $certfile";
  115. print "RUN: $cmdline\n" if($verbose);
  116. exec("exec $cmdline 2>$dev_null");