# Test reading fail when use https

**URL:** <https://www.ruby-forum.com/t/test-reading-fail-when-use-https/208728>\
**Category:** NGINX\
**Created:** [June 25, 2011, 4:50pm UTC](https://www.ruby-forum.com/t/test-reading-fail-when-use-https/208728 "2011-06-25T16:50:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [June 25, 2011, 4:50pm UTC](https://www.ruby-forum.com/t/test-reading-fail-when-use-https/208728/1 "2011-06-25T16:50:16Z")

</div>

Hi,

I use the function ngx\_http\_test\_reading in the module Nginx Push Stream  
Module ([GitHub - wandenberg/nginx-push-stream-module: A pure stream http push technology for your Nginx setup. Comet made easy and really scalable.](https://github.com/wandenberg/nginx-push-stream-module)) to know  
when the user goes off, but when using https this function always  
return a value on call  
n = recv(c-\>fd, buf, 1, MSG\_PEEK);

Checking what was happening and comparing with others calls on same file  
ngx\_http\_request.c I saw that others calls are done with c-\>recv instead  
of recv .

On the case of https this c-\>recv points to ngx\_ssl\_recv, which can sure  
detect when user goes off.

I would like to suggest to change the line  
n = recv(c-\>fd, buf, 1, MSG\_PEEK);  
for  
n = c-\>recv(c, buf, 1);

and  
} else if (n == -1) {  
for  
} else if ((n == -1) || (n == -2)) {

to other returns on https.

I’m not sure if it has any side effect, and don’t know if here is the  
best place to do this suggestion, sorry if not.

Regards,  
Wandenberg

Posted at Nginx Forum:

> **[test reading fail when use https](https://forum.nginx.org/read.php?2,209967,209967#msg-209967)**
>
> Hi, I use the function ngx\_http\_test\_reading in the module Nginx Push Stream Module (https://github.com/wandenberg/nginx-push-stream-module) to know when the user goes off, but when using https this function always return a value on call n =...

---

<div class="post-metadata">

**Author:** ![wandenberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/3da27b/32.png) [@wandenberg](https://www.ruby-forum.com/u/wandenberg)\
**Post date:** [June 25, 2011, 9:42pm UTC](https://www.ruby-forum.com/t/test-reading-fail-when-use-https/208728/2 "2011-06-25T21:42:09Z")

</div>

Hello!

On Sat, Jun 25, 2011 at 10:49:32AM -0400, wandenberg wrote:

> of recv .  
> } else if (n == -1) {  
> for  
> } else if ((n == -1) || (n == -2)) {
> 
> to other returns on https.
> 
> I’m not sure if it has any side effect, and don’t know if here is the  
> best place to do this suggestion, sorry if not.

There is a MSG\_PEEK for reason.

Maxim D.

---

<div class="post-metadata">

**Author:** ![wandenberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/3da27b/32.png) [@wandenberg](https://www.ruby-forum.com/u/wandenberg)\
**Post date:** [June 25, 2011, 10:06pm UTC](https://www.ruby-forum.com/t/test-reading-fail-when-use-https/208728/3 "2011-06-25T22:06:52Z")

</div>

Sorry Maxim. I didn’t understood your answer.

Can you explain better? Why this work for detect when users goes off on  
http, but not in https ?

Posted at Nginx Forum:

> **[Re: test reading fail when use https](https://forum.nginx.org/read.php?2,209967,210038#msg-210038)**
>
> Hi, I use the function ngx\_http\_test\_reading in the module Nginx Push Stream Module (https://github.com/wandenberg/nginx-push-stream-module) to know when the user goes off, but when using https this function always return a value on call n =...

---

<div class="post-metadata">

**Author:** ![wandenberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/3da27b/32.png) [@wandenberg](https://www.ruby-forum.com/u/wandenberg)\
**Post date:** [June 25, 2011, 10:48pm UTC](https://www.ruby-forum.com/t/test-reading-fail-when-use-https/208728/4 "2011-06-25T22:48:52Z")

</div>

Hello!

On Sat, Jun 25, 2011 at 04:06:30PM -0400, wandenberg wrote:

> Sorry Maxim. I didn’t understood your answer.

You can’t use c-\>recv() as it doesn’t allow to peek. Using  
c-\>recv() will result in possibly valid and required data to be  
lost. And that’s why normal recv() is used instead there.

> Can you explain better? Why this work for detect when users goes off on  
> http, but not in https ?

Quoting just 2 days old message from the same list[1]:

% Yes, the code in question won’t be able to detect SSL connection  
% close with appropriate shutdown records sent by a client (as well  
% as connection close with some other pending data, e.g. pipelined  
% request). It is not possible to detect connection close with  
% standard socket interface if there are pending data without  
% reading that data first, which isn’t always desired/possible.  
%  
% In the particular case of SSL it should be possible to use  
% SSL\_peek() here, but better aproach is to use OS-provided hint,  
% e.g. as EV\_EOF provided by kqueue interface (and used by nginx).

[1] [Is there a bug in ngx\_http\_upstream\_module's broken connection check code?](http://nginx.org/pipermail/nginx/2011-June/027672.html)

Maxim D.

---

<div class="post-metadata">

**Author:** ![wandenberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/3da27b/32.png) [@wandenberg](https://www.ruby-forum.com/u/wandenberg)\
**Post date:** [June 26, 2011, 3:28pm UTC](https://www.ruby-forum.com/t/test-reading-fail-when-use-https/208728/5 "2011-06-26T15:28:44Z")

</div>

Hello!

Thanks for your explanation.  
Now I understood, sorry for my mistakes.

## Maxim D. Wrote:

> required data to be  
> lost. And that’s why normal recv() is used  
> instead there.

I turned back to use normal recv() respecting msg\_peek functionality.

> a client (as well  
> possible to use  
> % SSL\_peek() here, but better aproach is to use  
> OS-provided hint,  
> % e.g. as EV\_EOF provided by kqueue interface (and  
> used by nginx).
> 
> [1]  
> [http://nginx.org/pipermail/nginx/2011-June/027672](http://nginx.org/pipermail/nginx/2011-June/027672).  
> html

To solve the problem of detect when users close connection on https I  
did this implementation

#if (NGX\_HTTP\_SSL)  
if (c-\>ssl == NULL) {  
n = recv(c-\>fd, buf, 1, MSG\_PEEK);  
} else {  
n = SSL\_peek(c-\>ssl-\>connection, buf, 1);  
}  
#else  
n = recv(c-\>fd, buf, 1, MSG\_PEEK);  
#endif

I would like to know if you see any other problem on that and if is  
possible to this code be applied on nginx source?

> Maxim D.
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

Thanks for your quickly response.

Regards,  
Wandenberg

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,209967,210178#msg-210178](http://forum.nginx.org/read.php?2,209967,210178#msg-210178)

---

<div class="post-metadata">

**Author:** ![wandenberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/3da27b/32.png) [@wandenberg](https://www.ruby-forum.com/u/wandenberg)\
**Post date:** [June 26, 2011, 9:53pm UTC](https://www.ruby-forum.com/t/test-reading-fail-when-use-https/208728/6 "2011-06-26T21:53:41Z")

</div>

Hello!

On Sun, Jun 26, 2011 at 09:28:29AM -0400, wandenberg wrote:

[…]

> ```
> n = recv(c->fd, buf, 1, MSG_PEEK);
> 
> ```
> 
> #endif
> 
> I would like to know if you see any other problem on that and if is  
> possible to this code be applied on nginx source?

SSL\_peek may (and likely will in many cases) leave an error in  
OpenSSL error queue, so I suspect this actually requires a bit  
more code.

Maxim D.

---

<div class="post-metadata">

**Author:** ![wandenberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/3da27b/32.png) [@wandenberg](https://www.ruby-forum.com/u/wandenberg)\
**Post date:** [July 4, 2011, 6:05pm UTC](https://www.ruby-forum.com/t/test-reading-fail-when-use-https/208728/7 "2011-07-04T18:05:21Z")

</div>

Hi Maxim,

now I think the code is ok.  
Could you check, please?

## Maxim D. Wrote:

> > I would like to know if you see any other  
> > Maxim D.
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

#if (NGX\_HTTP\_SSL)  
if (c-\>ssl != NULL) {  
n = SSL\_peek(c-\>ssl-\>connection, buf, 1);  
} else {  
n = recv(c-\>fd, buf, 1, MSG\_PEEK);  
}  
#else  
n = recv(c-\>fd, buf, 1, MSG\_PEEK);  
#endif

```
if (n == 0) {
    rev->eof = 1;
    c->error = 1;
    err = 0;

    goto closed;

} else if (n == -1) {

```

#if (NGX\_HTTP\_SSL)  
if (c-\>ssl != NULL) {  
err = SSL\_get\_error(c-\>ssl-\>connection, n);  
} else {  
err = ngx\_socket\_errno;  
}

```
if ((err != NGX_EAGAIN) && (err != SSL_ERROR_WANT_READ) && (err !=

```

SSL\_ERROR\_WANT\_WRITE)) {  
#else  
err = ngx\_socket\_errno;  
if (err != NGX\_EAGAIN) {  
#endif  
rev-\>eof = 1;  
c-\>error = 1;

```
        goto closed;
    }
}

```

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,209967,211939#msg-211939](http://forum.nginx.org/read.php?2,209967,211939#msg-211939)
