PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/curl/tests/serverhelp.pm

http://github.com/mono/moon
Perl | 238 lines | 124 code | 42 blank | 72 comment | 9 complexity | 7e4c65d9e8fc0cae0e4d4c9002ec4659 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-3.0
  1. #***************************************************************************
  2. # _ _ ____ _
  3. # Project ___| | | | _ \| |
  4. # / __| | | | |_) | |
  5. # | (__| |_| | _ <| |___
  6. # \___|\___/|_| \_\_____|
  7. #
  8. # Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. #
  10. # This software is licensed as described in the file COPYING, which
  11. # you should have received as part of this distribution. The terms
  12. # are also available at http://curl.haxx.se/docs/copyright.html.
  13. #
  14. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. # copies of the Software, and permit persons to whom the Software is
  16. # furnished to do so, under the terms of the COPYING file.
  17. #
  18. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. # KIND, either express or implied.
  20. #
  21. # $Id$
  22. #***************************************************************************
  23. package serverhelp;
  24. use strict;
  25. use warnings;
  26. use Exporter;
  27. #***************************************************************************
  28. # Global symbols allowed without explicit package name
  29. #
  30. use vars qw(
  31. @ISA
  32. @EXPORT_OK
  33. );
  34. #***************************************************************************
  35. # Inherit Exporter's capabilities
  36. #
  37. @ISA = qw(Exporter);
  38. #***************************************************************************
  39. # Global symbols this module will export upon request
  40. #
  41. @EXPORT_OK = qw(
  42. serverfactors
  43. servername_id
  44. servername_str
  45. servername_canon
  46. server_pidfilename
  47. server_logfilename
  48. server_cmdfilename
  49. server_inputfilename
  50. server_outputfilename
  51. mainsockf_pidfilename
  52. mainsockf_logfilename
  53. datasockf_pidfilename
  54. datasockf_logfilename
  55. );
  56. #***************************************************************************
  57. # Return server characterization factors given a server id string.
  58. #
  59. sub serverfactors {
  60. my $server = $_[0];
  61. my $proto;
  62. my $ipvnum;
  63. my $idnum;
  64. if($server =~ /^((ftp|http|imap|pop3|smtp)s?)(\d*)(-ipv6|)$/) {
  65. $proto = $1;
  66. $idnum = ($3 && ($3 > 1)) ? $3 : 1;
  67. $ipvnum = ($4 && ($4 =~ /6$/)) ? 6 : 4;
  68. }
  69. elsif($server =~ /^(tftp|sftp|socks|ssh|rtsp)(\d*)(-ipv6|)$/) {
  70. $proto = $1;
  71. $idnum = ($2 && ($2 > 1)) ? $2 : 1;
  72. $ipvnum = ($3 && ($3 =~ /6$/)) ? 6 : 4;
  73. }
  74. else {
  75. die "invalid server id: $server"
  76. }
  77. return($proto, $ipvnum, $idnum);
  78. }
  79. #***************************************************************************
  80. # Return server name string formatted for presentation purposes
  81. #
  82. sub servername_str {
  83. my ($proto, $ipver, $idnum) = @_;
  84. $proto = uc($proto) if($proto);
  85. die "unsupported protocol: $proto" unless($proto &&
  86. ($proto =~ /^(((FTP|HTTP|IMAP|POP3|SMTP)S?)|(TFTP|SFTP|SOCKS|SSH|RTSP))$/));
  87. $ipver = (not $ipver) ? 'ipv4' : lc($ipver);
  88. die "unsupported IP version: $ipver" unless($ipver &&
  89. ($ipver =~ /^(4|6|ipv4|ipv6|-ipv4|-ipv6)$/));
  90. $ipver = ($ipver =~ /6$/) ? '-IPv6' : '';
  91. $idnum = 1 if(not $idnum);
  92. die "unsupported ID number: $idnum" unless($idnum &&
  93. ($idnum =~ /^(\d+)$/));
  94. $idnum = '' unless($idnum > 1);
  95. return "${proto}${idnum}${ipver}";
  96. }
  97. #***************************************************************************
  98. # Return server name string formatted for identification purposes
  99. #
  100. sub servername_id {
  101. my ($proto, $ipver, $idnum) = @_;
  102. return lc(servername_str($proto, $ipver, $idnum));
  103. }
  104. #***************************************************************************
  105. # Return server name string formatted for file name purposes
  106. #
  107. sub servername_canon {
  108. my ($proto, $ipver, $idnum) = @_;
  109. my $string = lc(servername_str($proto, $ipver, $idnum));
  110. $string =~ tr/-/_/;
  111. return $string;
  112. }
  113. #***************************************************************************
  114. # Return file name for server pid file.
  115. #
  116. sub server_pidfilename {
  117. my ($proto, $ipver, $idnum) = @_;
  118. my $trailer = '_server.pid';
  119. return '.'. servername_canon($proto, $ipver, $idnum) ."$trailer";
  120. }
  121. #***************************************************************************
  122. # Return file name for server log file.
  123. #
  124. sub server_logfilename {
  125. my ($logdir, $proto, $ipver, $idnum) = @_;
  126. my $trailer = '_server.log';
  127. $trailer = '_stunnel.log' if(lc($proto) =~ /^(ftp|http|imap|pop3|smtp)s$/);
  128. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  129. }
  130. #***************************************************************************
  131. # Return file name for server commands file.
  132. #
  133. sub server_cmdfilename {
  134. my ($logdir, $proto, $ipver, $idnum) = @_;
  135. my $trailer = '_server.cmd';
  136. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  137. }
  138. #***************************************************************************
  139. # Return file name for server input file.
  140. #
  141. sub server_inputfilename {
  142. my ($logdir, $proto, $ipver, $idnum) = @_;
  143. my $trailer = '_server.input';
  144. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  145. }
  146. #***************************************************************************
  147. # Return file name for server output file.
  148. #
  149. sub server_outputfilename {
  150. my ($logdir, $proto, $ipver, $idnum) = @_;
  151. my $trailer = '_server.output';
  152. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  153. }
  154. #***************************************************************************
  155. # Return file name for main or primary sockfilter pid file.
  156. #
  157. sub mainsockf_pidfilename {
  158. my ($proto, $ipver, $idnum) = @_;
  159. die "unsupported protocol: $proto" unless($proto &&
  160. (lc($proto) =~ /^(ftp|imap|pop3|smtp)s?$/));
  161. my $trailer = (lc($proto) =~ /^ftps?$/) ? '_sockctrl.pid':'_sockfilt.pid';
  162. return '.'. servername_canon($proto, $ipver, $idnum) ."$trailer";
  163. }
  164. #***************************************************************************
  165. # Return file name for main or primary sockfilter log file.
  166. #
  167. sub mainsockf_logfilename {
  168. my ($logdir, $proto, $ipver, $idnum) = @_;
  169. die "unsupported protocol: $proto" unless($proto &&
  170. (lc($proto) =~ /^(ftp|imap|pop3|smtp)s?$/));
  171. my $trailer = (lc($proto) =~ /^ftps?$/) ? '_sockctrl.log':'_sockfilt.log';
  172. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  173. }
  174. #***************************************************************************
  175. # Return file name for data or secondary sockfilter pid file.
  176. #
  177. sub datasockf_pidfilename {
  178. my ($proto, $ipver, $idnum) = @_;
  179. die "unsupported protocol: $proto" unless($proto &&
  180. (lc($proto) =~ /^ftps?$/));
  181. my $trailer = '_sockdata.pid';
  182. return '.'. servername_canon($proto, $ipver, $idnum) ."$trailer";
  183. }
  184. #***************************************************************************
  185. # Return file name for data or secondary sockfilter log file.
  186. #
  187. sub datasockf_logfilename {
  188. my ($logdir, $proto, $ipver, $idnum) = @_;
  189. die "unsupported protocol: $proto" unless($proto &&
  190. (lc($proto) =~ /^ftps?$/));
  191. my $trailer = '_sockdata.log';
  192. return "${logdir}/". servername_canon($proto, $ipver, $idnum) ."$trailer";
  193. }
  194. #***************************************************************************
  195. # End of library
  196. 1;