understanding ngx_str_t structure

classic Classic list List threaded Threaded
7 messages Options
xrd
Reply | Threaded
Open this post in threaded view
|

understanding ngx_str_t structure

xrd
Hi,

I trying to print the requested URL path. For that I came across the "uri"
member which is of type ngx_str_t) under ngx_http_request_t structure. When
I print the "uri" using ngx_log_debug1 (ngx_log_debug1(NGX_LOG_DEBUG_HTTP,
log, 0, "%s", r->uri.data)) I see some extra data printed.

For eg. when http://localhost/test is requested, "/test HTTP/1.1 Host" is
printed.

In my understanding "uri.data" should have contained only "/test". The
"uri.len" correctly shows the length as 5. The "data" member is unsigned
char *data so while extracting the actual string do we need to consider the
length in ngx_str_t ?

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,285729,285729#msg-285729

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: understanding ngx_str_t structure

Roman Arutyunyan
Hi,

On Fri, Sep 27, 2019 at 05:44:09AM -0400, astre wrote:

> Hi,
>
> I trying to print the requested URL path. For that I came across the "uri"
> member which is of type ngx_str_t) under ngx_http_request_t structure. When
> I print the "uri" using ngx_log_debug1 (ngx_log_debug1(NGX_LOG_DEBUG_HTTP,
> log, 0, "%s", r->uri.data)) I see some extra data printed.
>
> For eg. when http://localhost/test is requested, "/test HTTP/1.1 Host" is
> printed.
>
> In my understanding "uri.data" should have contained only "/test". The
> "uri.len" correctly shows the length as 5. The "data" member is unsigned
> char *data so while extracting the actual string do we need to consider the
> length in ngx_str_t ?

The %s format suggests that the string is null-terminated, which is not the
case for r->uri.data.  There's a special format %V for printing ngx_str_t:

  ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "%V", &r->uri);

For more information on ngx_str_t, see the dev guide:

http://nginx.org/en/docs/dev/development_guide.html#strings

> Posted at Nginx Forum: https://forum.nginx.org/read.php?2,285729,285729#msg-285729
>
> _______________________________________________
> nginx mailing list
> [hidden email]
> http://mailman.nginx.org/mailman/listinfo/nginx

--
Roman Arutyunyan
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
xrd
Reply | Threaded
Open this post in threaded view
|

Re: understanding ngx_str_t structure

xrd
Thanks Roman, it is working. Is there a way to directly print using
std::cout instead of ngx_log_* functions ?
P.S: I'm writing module in C++.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,285729,285731#msg-285731

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: understanding ngx_str_t structure

Roman Arutyunyan
On Fri, Sep 27, 2019 at 06:51:12AM -0400, astre wrote:
> Thanks Roman, it is working. Is there a way to directly print using
> std::cout instead of ngx_log_* functions ?
> P.S: I'm writing module in C++.

You can use string length in the 'len' field to limit the number of characters
printed.  There are many ways to do that, for example:

std::cout.write(r->uri.data, r->uri.len);

>
> Posted at Nginx Forum: https://forum.nginx.org/read.php?2,285729,285731#msg-285731
>
> _______________________________________________
> nginx mailing list
> [hidden email]
> http://mailman.nginx.org/mailman/listinfo/nginx

PS: it is better to ask development questions in the nginx-devel mailing list:
http://mailman.nginx.org/mailman/listinfo/nginx-devel

--
Roman Arutyunyan
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
xrd
Reply | Threaded
Open this post in threaded view
|

Re: understanding ngx_str_t structure

xrd
Right, actually the question is when I use std::cout nothing gets printed in
error_log even when it is set to debug.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,285729,285733#msg-285733

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: understanding ngx_str_t structure

Roman Arutyunyan
On Fri, Sep 27, 2019 at 07:17:59AM -0400, astre wrote:
> Right, actually the question is when I use std::cout nothing gets printed in
> error_log even when it is set to debug.

Logging in nginx is more complicated than just writing to fd #1.  If you want to
log a message to nginx error log, you should use nginx logging API.

> Posted at Nginx Forum: https://forum.nginx.org/read.php?2,285729,285733#msg-285733
>
> _______________________________________________
> nginx mailing list
> [hidden email]
> http://mailman.nginx.org/mailman/listinfo/nginx

--
Roman Arutyunyan
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
xrd
Reply | Threaded
Open this post in threaded view
|

Re: understanding ngx_str_t structure

xrd
ok thanks, then probably write to syslog using syslog API's and it seems
nginx already supports syslog. Will that work?

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,285729,285737#msg-285737

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx