Recent

Author Topic: Problem with TResponse.SendRedirect  (Read 716 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1493
Problem with TResponse.SendRedirect
« on: October 23, 2024, 01:17:18 pm »
In my webserver program,

AResponse.SendRedirect does not always work.  The target page is static page. The error message (from IIS) says,

Quote
HTTP Error 405.0 - Method Not Allowed
Page cannot be displayed as wrong method (HTTP verb) is being used.

nginx says the same error.

The static page itself is displayed without any problem.

And SendRedirect is also done correctly when I directly put it in the ActionRequest --- only through GET method. 

It semms that after the web server has received something via POST method, then SendRedirect does not work.

What should I check, and what could be solutions?

rvk

  • Hero Member
  • *****
  • Posts: 6585
Re: Problem with TResponse.SendRedirect
« Reply #1 on: October 23, 2024, 01:31:36 pm »
I'm not sure what the problem is (you don't show much code).

But TResponse.SendRedirect itself doesn't do much.
It just sets the Location header to TargetURL and Code/CodeText to 307 or 302.
That is send back to the client.

What the client does with it... is up to the client.

If the code is redirect/moved... maybe the client wants to do the POST to that new URL.
Can your redirected URL (static page) handle POST ??

You could try to do a post to it via curl, just post some dummy data to that page.

Next... what is actually the method you receive at that IIS and nginx server when you receive that error.
(should be in the logs)

egsuh

  • Hero Member
  • *****
  • Posts: 1493
Re: Problem with TResponse.SendRedirect
« Reply #2 on: October 23, 2024, 03:28:58 pm »
This is rather funny...  I made a simple example. You may try this on:

http://www.alphaq.biz/htmls/p121_002.htm

Code: Text  [Select][+][-]
  1. <!DOCType HTML>
  2. <head>
  3. <title>프로그램 테스트</title>
  4. <meta charset=utf-8>
  5.   <meta name="viewport" content="width=device-width">
  6.  </head>
  7. <body>
  8. <br>
  9. Your name: <br><br>
  10.  
  11. <form action='http://www.alphaq.biz/betas/aq02cb/e/test' method=post>
  12. Within Post method form :
  13.  <button onclick="location.href='http://www.alphaq.biz/betas/aq02cb/e/test'">Within POST form</button>
  14. <br>
  15. </form>
  16. <br>
  17. <br>
  18. <form action='http://www.alphaq.biz/betas/aq02cb/e/test' method=get>
  19. Within Get method form:
  20.  <button onclick="location.href='http://www.alphaq.biz/betas/aq02cb?kc=TEST_0014'">Within GET form</button>
  21. </form>
  22. </body>
  23. </html>

AQ02cb/e/test has following code:

Code: Pascal  [Select][+][-]
  1. procedure TwmAQe.TestRequest(Sender: TObject; ARequest: TRequest;
  2.    AResponse: TResponse; var Handled: Boolean);
  3. begin
  4.    Aresponse.SendRedirect(Format('http://%s/htmls/p121_002.htm', [ARequest.Host]));
  5.    Handled:= True;
  6. end;
  7.  

When you press the second button, you'll see the same page again.
But when you press the first button, you'll get error message.

BTW, button's onclick link seems to do nothing in this case.

rvk

  • Hero Member
  • *****
  • Posts: 6585
Re: Problem with TResponse.SendRedirect
« Reply #3 on: October 23, 2024, 03:46:33 pm »
The page http://www.alphaq.biz/htmls/p121_002.htm doesn't allow POST.
According to the headers, only GET, HEAD, OPTIONS, TRACE is allowed.

So if you do a POST to http://www.alphaq.biz/betas/aq02cb/e/test
and that page gives a redirect... the browsers follows this up with a POST to that redirected URL.

Who is serving the page http://www.alphaq.biz/htmls/p121_002.htm and why is a POST to that page set as 'not allowed'?

Quote
HTTP/1.1 405 Method Not Allowed
Allow: GET, HEAD, OPTIONS, TRACE
Content-Type: text/html
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET
Date: Wed, 23 Oct 2024 13:42:20 GMT

Content-Length: 1307

So dive into that Microsoft-IIS and allow a POST to that page.

And if this is really not a page where you can POST to... then you SHOULDN'T use the 307 redirect.
Quote
15.4.8. 307 Temporary Redirect
The 307 (Temporary Redirect) status code indicates that the target resource resides temporarily under a different URI and the user agent MUST NOT change the request method if it performs an automatic redirection to that URI. Since the redirection can change over time, the client ought to continue using the original target URI for future requests.

The server SHOULD generate a Location header field in the response containing a URI reference for the different URI. The user agent MAY use the Location field value for automatic redirection. The server's response content usually contains a short hypertext note with a hyperlink to the different URI(s).
https://www.rfc-editor.org/rfc/rfc9110.html#name-307-temporary-redirect

egsuh

  • Hero Member
  • *****
  • Posts: 1493
Re: Problem with TResponse.SendRedirect
« Reply #4 on: October 24, 2024, 03:17:01 am »
I gave up looking into IIS and nginx. Instead, I let my webserver program handle the file.

Code: Pascal  [Select][+][-]
  1. procedure TwmAQe.TestRequest(Sender: TObject; ARequest: TRequest;
  2.    AResponse: TResponse; var Handled: Boolean);
  3. begin
  4.    AResponse.Content := Actions.ActionByName('Test').Template.GetContent;
  5.    Handled:= True;
  6. end;

or

    AResponse.Contents.LoadFromFile(Actions.ActionByName('Test').Template.Filename;

works as well.


But are these the best solution?  I assigned the filename to the Action's Template's filename. Then should it be automatically loaded simply by AResponse.SendRespond; ?

rvk

  • Hero Member
  • *****
  • Posts: 6585
Re: Problem with TResponse.SendRedirect
« Reply #5 on: October 24, 2024, 10:41:11 am »
I gave up looking into IIS and nginx. Instead, I let my webserver program handle the file.
Did you read the redirection-3xx section?  ;)

You could have just given a 302 'Found' back to the client after which the client would do a GET to that page.
The 307 is defined as redirect that the client POST will keep POST, but 301 and 302 will change to GET.
So with a 302 the client would have done a GET to that page (which was allowed).

https://www.rfc-editor.org/rfc/rfc9110.html#name-redirection-3xx

I assigned the filename to the Action's Template's filename. Then should it be automatically loaded simply by AResponse.SendRespond; ?
I'm not familiar with Action's templates... but I don't see TResponse.SendResponse doing anything automatically.
(SendResponse is for Delphi compatibility and it just calls TResponse.SendContent, which just sends the Content)
« Last Edit: October 24, 2024, 10:43:09 am by rvk »

 

TinyPortal © 2005-2018