PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/CXGN/Apache/Request.pm

https://github.com/solgenomics/sgn
Perl | 136 lines | 107 code | 26 blank | 3 comment | 12 complexity | 51d69d305c78404f527993ac1d57695d MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  1. =head1 NAME
  2. CXGN::Apache::Request
  3. =head1 DESCRIPTION
  4. This module is used mainly by CXGN::Apache::Error to provide detailed information about who caused a page error and how they caused it.
  5. =head1 NON-OBJECT METHODS
  6. =head2 as_verbose_string
  7. A more detailed version of Apache::Request::as_string. It returns a big string full of information about who is requesting a page, how they are requesting it, etc.
  8. =head2 client_name
  9. Get the name of the client and also note if the client is from SGN by returning a second parameter.
  10. =head2 page_name
  11. Returns the page_name by extracting it from the current page request. Note: /tools/does_not_exist.pl needs it to be done this way, rather than asking for this information from perl environment variables, because "does_not_exist.pl" (the page the client was sent to) would be returned by the environment variables, rather than the page the client originally requested.
  12. =head2 time
  13. Returns a string indicating the time in Ithaca NY. Used to associate a time with an error report.
  14. =head1 AUTHOR
  15. john binns - John Binns <zombieite@gmail.com>
  16. =cut
  17. package CXGN::Apache::Request;
  18. use strict;
  19. use Data::Dumper;
  20. use URI::Escape;
  21. use Socket; #used to get hostname from IP
  22. our $CORNELL_IP_MATCH = '132.236.157.';
  23. our $OTHER_KNOWN_IPS = {
  24. '127.0.0.1' => 'localhost',
  25. '24.22.41.189' => 'Rob'
  26. };
  27. sub as_verbose_string {
  28. my $verbose_string = '';
  29. my $time = &time();
  30. my ( $client_name, $cornell_client_name ) = &client_name();
  31. my ( $page_name, $parameters ) = &page_name();
  32. if ($parameters) {
  33. $parameters =~ s/&amp;/\n/g;
  34. $parameters =~ s/&/\n/g;
  35. $parameters =~ s/;/\n/g;
  36. }
  37. # my $login=CXGN::Login->new();
  38. # my $id=$login->has_session();
  39. $verbose_string .= "\nrequest\n--------------\n";
  40. $verbose_string .=
  41. "$page_name on SGN requested by $client_name at $time\n\n";
  42. if ($parameters) { $verbose_string .= "with parameters:\n$parameters\n\n"; }
  43. # # if($id){$verbose_string.="with login ID: $id\n\n";}
  44. $verbose_string .= "Request Environment:\n";
  45. $verbose_string .= Dumper(\%ENV);
  46. return $verbose_string;
  47. }
  48. sub time {
  49. my ( $sec, $min, $hour, $day, $month, $year ) = localtime();
  50. $sec = sprintf( "%02d", $sec );
  51. $min = sprintf( "%02d", $min );
  52. $month += 1;
  53. $year = sprintf( "%02d", $year % 100 );
  54. return "$month/$day-$hour:$min:$sec";
  55. }
  56. sub client_name {
  57. my $client_name = my $remote_host = CGI->new->remote_host;
  58. my $known_client_name;
  59. if ( $remote_host =~ /$CORNELL_IP_MATCH/ ) {
  60. if ( $client_name =~ /(\w+)\.sgn\.cornell\.edu/ ) {
  61. $known_client_name = $1;
  62. }
  63. else {
  64. $known_client_name = $client_name;
  65. }
  66. }
  67. else {
  68. $known_client_name = $OTHER_KNOWN_IPS->{$remote_host};
  69. }
  70. return ( $client_name, $known_client_name );
  71. }
  72. sub full_page_name {
  73. my ($request_string) = @_
  74. ; #you can send in a string if you want (does_not_exist.pl does this for reasons of its own)
  75. my $parameter_string = '';
  76. $request_string ||= CGI->new->url( -query => 1 );
  77. if ( $request_string =~ / (.+) /i
  78. ) #if the request says "GET /cgi-bin/mypage.pl?arg=1 HTTP/1.1" we want the "/cgi-bin/mypage.pl?arg=1" between the space characters
  79. {
  80. $request_string = $1;
  81. }
  82. $request_string = URI::Escape::uri_unescape($request_string)
  83. ; #usually unnecessary, but occasionally we get wacky encoded requests
  84. if ( $request_string =~
  85. /(.+)\?(.*)/ ) #if the request string has parameters, remove them
  86. {
  87. $request_string = $1;
  88. $parameter_string = $2;
  89. }
  90. return ( $request_string, $parameter_string );
  91. }
  92. sub page_name {
  93. my ($request_string) = @_
  94. ; #you can send in a string if you want (does_not_exist.pl does this for reasons of its own)
  95. my $parameter_string = '';
  96. ( $request_string, $parameter_string ) = full_page_name($request_string);
  97. unless ($parameter_string) { $parameter_string = ''; }
  98. if ( $request_string =~
  99. /\/((\w|-)+\.(pl|html|htm|js|gif|jpg|css|png|ppt|xls|pdf|cgi))/i
  100. ) #if we can recognize the script name in the request string, get it
  101. {
  102. if ( $1 ne 'index.pl'
  103. ) #index.pl is not descriptive enough, so don't use it if that's all we have
  104. {
  105. $request_string = $1;
  106. }
  107. }
  108. return ( $request_string, $parameter_string );
  109. }
  110. 1;