response header location ignored

35 views
Skip to first unread message

Andries

unread,
Dec 6, 2014, 4:40:54 AM12/6/14
to nx...@googlegroups.com
Hi Yaroslav,

I try to jump to another page as follows, but the redirect is not done:

nxweb_add_response_header_safe(resp, "Location", new_location);

Is location header supported in nxweb? Or what do you suggest to redirect. I need to say: the redirect is executed in the code, after certain conditions.

Andries

Yaroslav

unread,
Dec 6, 2014, 5:37:04 AM12/6/14
to nx...@googlegroups.com
Hi Andries,

There are two special functions for redirects:

void nxweb_send_redirect(nxweb_http_response *resp, int code, const char* location, int secure);
void nxweb_send_redirect2(nxweb_http_response *resp, int code, const char* location, const char* location_path_info, int secure);

The second one could be handy when you build location from two parts (domain + path_info).

On the other hand nxweb_add_response_header_safe() should work as well. Did you analyse nxweb response with `curl -v http://...`?

Yaroslav



--
You received this message because you are subscribed to the Google Groups "nxweb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nxweb+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andries

unread,
Dec 11, 2014, 2:27:35 PM12/11/14
to nx...@googlegroups.com
With nxweb_send_redirect() and nxweb_send_redirect2() I get his error:
unfinished stream found in nxb 0x7f4918005bd0

Here is the code I tried:
nxweb_send_redirect(resp, 301, "/", conn->secure);
nxweb_start_sending_response(conn, resp);
return NXWEB_OK;

I prefer the response header way, but this also doesn't work. Here is a curl -v response:
> GET /test HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.16.1 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: myhost
> Accept: */*
< HTTP/1.1 200 OK
< Server: nxweb/3.3.0-dev
< Date: Thu, 11 Dec 2014 18:57:17 GMT
< Connection: keep-alive
< Location: /
< Content-Type: text/html
< Content-Length: 18

As you can see, the Location header is added, but no redirect took place.

Andries

unread,
Dec 11, 2014, 2:34:13 PM12/11/14
to nx...@googlegroups.com
I found my problem. I needed to set the resp->status_code to 301. Now it worked (with header method)

Yaroslav

unread,
Dec 11, 2014, 2:58:18 PM12/11/14
to nx...@googlegroups.com
On Thu, Dec 11, 2014 at 10:27 PM, Andries <andrie...@gmail.com> wrote:
With nxweb_send_redirect() and nxweb_send_redirect2() I get his error:
unfinished stream found in nxb 0x7f4918005bd0

This means you have started writing response content before calling this function. It's better not to do that, otherwise you have to unfinish it before returning from your handler.
 

Here is the code I tried:
nxweb_send_redirect(resp, 301, "/", conn->secure);
 
nxweb_start_sending_response(conn, resp); - this is not needed (although should not harm)

You don't need to call this at all in on_request handler. It is automatically called upon return from your on_request handler. It could be needed in on_select handler in case you wish to start sending response prematurely (skip on_request).

return NXWEB_OK;

I prefer the response header way, but this also doesn't work. Here is a curl -v response:
> GET /test HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.16.1 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: myhost
> Accept: */*
< HTTP/1.1 200 OK
< Server: nxweb/3.3.0-dev
< Date: Thu, 11 Dec 2014 18:57:17 GMT
< Connection: keep-alive
< Location: /
< Content-Type: text/html
< Content-Length: 18

As you can see, the Location header is added, but no redirect took place.

As you have already figured it out, 301 or 302 status code is required by HTTP protocol for redirect to take place.
Reply all
Reply to author
Forward
0 new messages