daily pastebin goal
53%
SHARE
TWEET

Untitled

a guest May 16th, 2018 110 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #summary One-sentence summary of this page.
  2.  
  3. = Introduction =
  4. Adding this in since this does not contain the code.  
  5.  
  6. = Details =
  7. #!/usr/bin/perl -w
  8. # Jeff Torgerson
  9. # chee-Z WebServer
  10. # TODO:
  11. # [] parse parameters
  12. # [] pass arguments to web pages, and regex replace the values
  13. # [] config for base directory, port, etc
  14.  
  15. use strict;
  16. use IO::Socket;
  17. use Time::Local;
  18.  
  19. #####################
  20. # declarations
  21. #####################
  22. my ($client,$cheezServer,$count,$jCount,$linenumber,$line,$logfile,$fileToServe,$ourMsg,$time,$timestamp,$fnf,$year);
  23. my (@date,@info,@lines,@ln);
  24.  
  25. #####################
  26. ## Settings
  27. #####################
  28. $logfile = "chee-z_webserver.log";
  29.  
  30. #####################
  31. # main
  32. #####################
  33. $cheezServer = IO::Socket::INET->new
  34. (
  35. LocalPort => 80,
  36. Type => SOCK_STREAM,
  37. Reuse => 1,
  38. Listen => 5 # number of clients to allow
  39. ) or die "Could not open your dumb server.. es ist kaput!";
  40.  
  41. # Now, wait for a connection
  42. while ($client = $cheezServer->accept()) {
  43. # $client->autoflush(1);
  44.  
  45. my $request = <$client>; # Get the first line
  46. # right below here i get strange error on command line hits
  47. # Use of uninitialized value in pattern match (m//) at E:\scripts\webserver.pl line 40.
  48. if ($request =~ m|^GET /(.+)HTTP/1.[01]|) { # this must match a valid http request
  49. my $theActualFile = stripParams($1); # I need to get rid of the params passed
  50. # writeLogFile("debug","$theActualFile");# This allows me to throw a debug msg into the log file
  51. if (-e $theActualFile) { # does the file exist
  52. print $client "HTTP/1.0 200 OK\nContent-Type: text/html\n\n"; # Start serving my request
  53.  
  54. open($fileToServe,"<$theActualFile") or die "Can't open file: $theActualFile";
  55. while(<$fileToServe>) { print $client $_ };
  56. # now write the log file
  57. writeLogFile("200","$1");
  58. } else {
  59. # we have a file not found if we get here, 404 error
  60. $fnf = notFound("$theActualFile");
  61. print $client $fnf; # writes the not found page
  62. }
  63. } else {
  64. # we did not get a valid request
  65. print $client "HTTP/1.0 400 BAD REQUEST\n";
  66. print $client "Content-Type: text/plain\n\n";
  67. print $client "BAD REQUEST\n";
  68. }
  69. close $client;
  70. }
  71.  
  72. # now, close the server
  73. close($cheezServer);
  74.  
  75. ############################################################
  76. ## Subroutines
  77. ############################################################
  78.  
  79.  
  80. ##############################
  81. ## 404 errors here
  82. sub notFound{
  83. my $theFile = $_[0];
  84. $ourMsg = "HTTP/1.0 404 FILE NOT FOUND\n";
  85. $ourMsg = $ourMsg."Content-Type: text/html\n\n";
  86. $ourMsg = $ourMsg."<h2>404 - File Not found</h2><b>".$theFile."</b> not found\n";
  87. writeLogFile("404","$theFile");
  88. return $ourMsg;
  89. }
  90.  
  91. ##############################
  92. ## Write my log file
  93. sub writeLogFile{
  94. my $httpStatusCode = $_[0];
  95. my $entry = $_[1];
  96.  
  97. # now, open the log file
  98. open(cheeZjournal,">>$logfile" ) || die "Could not open journal"; # Open the file
  99. @lines = <cheeZjournal>;
  100. @info = stat cheeZjournal;
  101. @date = localtime($info[9]);
  102. $year = sprintf("%d-%d-%d",$date[4]+1,$date[3],$date[5]+1900);
  103. $time = "$date[2]:$date[1]"; # goofy error here when time is 7:03, I get 7:3
  104. $timestamp = "$year|$time||$info[9]||";
  105. $linenumber = $count;
  106. print cheeZjournal "$httpStatusCode|$timestamp|$entry\n";
  107. close(cheeZjournal) ; # Close the file
  108. print @lines;
  109. }
  110.  
  111. ##############################
  112. ## Strip all the parameters
  113. ## off the file name for use
  114. sub stripParams{
  115. my @request;
  116. my $fullStuff = $_[0];
  117. # split on the Q to get only the file name
  118. @request = split(/\?/,$fullStuff);
  119. return $request[0]
  120. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top