这是一个本人学习 csapp 的 learning 库
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

138 рядки
3.5 KiB

2 роки тому
  1. ###############################################################
  2. # Driverlib.pm - A package of helper functions for Perl drivers
  3. #
  4. # Copyright (c) 2005 David R. O'Hallaron, All rights reserved.
  5. ###############################################################
  6. package Driverlib;
  7. use Socket;
  8. # Autogenerated header file with lab-specific constants
  9. use lib ".";
  10. use Driverhdrs;
  11. require Exporter;
  12. @ISA = qw(Exporter);
  13. @EXPORT = qw(
  14. driver_post
  15. );
  16. use strict;
  17. #####
  18. # Public functions
  19. #
  20. #
  21. # driver_post - This is the routine that a driver calls when
  22. # it needs to transmit an autoresult string to the result server.
  23. #
  24. sub driver_post ($$) {
  25. my $userid = shift; # User id for this submission
  26. my $result = shift; # Autoresult string
  27. my $autograded = shift; # Set if called by an autograder
  28. # Echo the autoresult string to stdout if the driver was called
  29. # by an autograder
  30. if ($autograded) {
  31. print "\n";
  32. print "AUTORESULT_STRING=$result\n";
  33. return;
  34. }
  35. # If the driver was called with a specific userid, then submit
  36. # the autoresult string to the result server over the Internet.
  37. if ($userid) {
  38. my $status = submitr($Driverhdrs::SERVER_NAME,
  39. $Driverhdrs::SERVER_PORT,
  40. $Driverhdrs::COURSE_NAME,
  41. $userid,
  42. $Driverhdrs::LAB,
  43. $result);
  44. # Print the status of the transfer
  45. if (!($status =~ /OK/)) {
  46. print "$status\n";
  47. print "Did not send autoresult string to the result server.\n";
  48. exit(1);
  49. }
  50. print "Success: Sent autoresult string for $userid to the result server.\n";
  51. }
  52. }
  53. #####
  54. # Private functions
  55. #
  56. #
  57. # submitr - Sends an autoresult string to the result server
  58. #
  59. sub submitr ($$$$$$) {
  60. my $hostname = shift;
  61. my $port = shift;
  62. my $course = shift;
  63. my $userid = shift;
  64. my $lab = shift;
  65. my $result = shift;
  66. my $internet_addr;
  67. my $enc_result;
  68. my $paddr;
  69. my $line;
  70. my $http_version;
  71. my $errcode;
  72. my $errmsg;
  73. # Establish the connection to the server
  74. socket(SERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp'));
  75. $internet_addr = inet_aton($hostname)
  76. or die "Could not convert $hostname to an internet address: $!\n";
  77. $paddr = sockaddr_in($port, $internet_addr);
  78. connect(SERVER, $paddr)
  79. or die "Could not connect to $hostname:$port:$!\n";
  80. select((select(SERVER), $| = 1)[0]); # enable command buffering
  81. # Send HTTP request to server
  82. $enc_result = url_encode($result);
  83. print SERVER "GET /$course/submitr.pl/?userid=$userid&lab=$lab&result=$enc_result&submit=submit HTTP/1.0\r\n\r\n";
  84. # Get first HTTP response line
  85. $line = <SERVER>;
  86. chomp($line);
  87. ($http_version, $errcode, $errmsg) = split(/\s+/, $line);
  88. if ($errcode != 200) {
  89. return "Error: HTTP request failed with error $errcode: $errmsg";
  90. }
  91. # Read the remaining HTTP response header lines
  92. while ($line = <SERVER>) {
  93. if ($line =~ /^\r\n/) {
  94. last;
  95. }
  96. }
  97. # Read and return the response from the result server
  98. $line = <SERVER>;
  99. chomp($line);
  100. close SERVER;
  101. return $line;
  102. }
  103. #
  104. # url_encode - Encode text string so it can be included in URI of GET request
  105. #
  106. sub url_encode ($) {
  107. my $value = shift;
  108. $value =~s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
  109. return $value;
  110. }
  111. # Always end a module with a 1 so that it returns TRUE
  112. 1;