how get response http header?

farzane

my last question was about how to implement basic authentication in perl and i got my answer. after that i tried to write my code.i used -status => '401 Not Authorized' in my http header and when i try to open my programm it wants me to enter user and password.in my code i got peice of header with ENV variable that include this username and password and check if it was what i want.my problem is that when i enter user and password in authentication box like below basic authentication

i should click on cancel button to get response header!!so what is ok button here? its my verification code

print header(-type => 'text/html', -status => '401 Not Authorized',
         'WWW-Authenticate' => 'Basic realm="Videos"');

print "<HTML>";
print "<HEAD>";

print "<TITLE>this is Test-Case</TITLE>";
print "</HEAD>";

my $signin = $ENV{HTTP_AUTHORIZATION};
my($basic,$userpass) = split( ' ', $signin );
($userpass,$eq) = split( '=',$userpass );
$userpass = decode_base64($userpass);
my ($user,$pass) =  split( ':',$userpass );
my $query = new CGI;
if($user eq 'aa' and $pass eq 'aa'){
show something
}
else{
     print "wrong user or pass";
}

i tried to use CGI::Auth::Basic before but it doesnt work for me and show error in module.

Thanks for your answers.

i solved my problem after a while so i decided to tell the answer for who have this problem too. you should firs check if $ENV{HTTP_AUTHORIZATION} is defined or not.if its defined you should check the user pass and if its true you print "Content-Type: text/HTML", "\n\n" that means 200ok!and if the ENV not defined you should print print header(-type => 'text/html', -status => '401 Not Authorized','WWW-Authenticate' => 'Basic realm="Videos"') to show the authentication box.

    $signin = $ENV{HTTP_AUTHORIZATION};
    if(defined $signin){
       check user and password here
       if(true user and password){
          print "Content-Type: text/HTML", "\n\n";
          do your all works here
       }
       else{
          wrong password
       }
    }
    else{
         print header(-type => 'text/html', -status => '401 Not   Authorized','WWW-Authenticate' => 'Basic realm="Videos"');
    }
Sebastian

HTTP Basic Auth works in two steps:

First step:

  • Browser sends a request
  • Server replies with a full HTTP Response (header, body) with HTTP status code 401
  • Browser shows a (browser-specific) dialog to ask for username and password
  • "OK" on that dialog typicalls starts step 2
  • "Cancel" on that dialog typically shows the response body received earlier - but that depends on the browser implementation. Don't rely on it!

Second step:

  • Browser re-sends the original request again, but adds an Authorization header
  • Server checks username and password and sends a full response (header, body) with either HTTP status code 200 (OK) or 401 (in this case: "username or password wrong, try again")
  • For code 401: See browser behavior for step 1
  • For code 200: Show the website as usual
  • Any other code is also valid: A 302 to redirect the user, a 500 to show an error, etc.

Hope that answers your question. If not, I didn't understand your problem.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how get response http header?

From Dev

Restangular: How to get HTTP response header?

From Dev

How to get http response header in VBScript

From Dev

How to get http response header in VBScript

From Dev

Restangular: How to get HTTP response header?

From Dev

Get HTTP Response header on UIWebView

From Dev

Get a header from HTTP response

From Dev

How to get Response Data and parse it to HTTP Header Manager in JMeter

From Dev

How to get Response Data and parse it to HTTP Header Manager in JMeter

From Dev

How to set a header in an HTTP response?

From Dev

How get curlpp response header

From Dev

How to alter HTTP Response header categorization in Fiddler

From Dev

How to send custom HTTP header in response?

From Dev

How to add header to http response in Spring HandlerInterceptorAdapter?

From Dev

angular2 How to store response header data from http.get in extra variable?

From Dev

How to get the response content of an HTTP 404 response

From Dev

how to get response of http request

From Dev

php - how to get the server response header and body

From Dev

How to get response header in react native android?

From Dev

How to get the response header in a RestEasy client?

From Dev

AngularJS $http response header

From Dev

AngularJS $http response header

From Dev

http response Header information

From Dev

adding http response header

From Dev

Get HTTP response header using Python requests module

From Dev

Get http-response header with Perl's File::Fetch

From Dev

android-async-http library get http response header from Response Handler

From Dev

Get response header OkHttp

From Dev

how to add a location header to http response from codeigniter rest server

Related Related

HotTag

Archive