Skip to content

Commit fefde40

Browse files
committed
upipe_http_source: improve error messages
1 parent 98be607 commit fefde40

File tree

5 files changed

+199
-33
lines changed

5 files changed

+199
-33
lines changed

include/upipe-modules/upipe_http_source.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,17 @@ struct upipe_http_src_hook {
5252

5353
struct {
5454
/** called when the transport socket is ready for read */
55-
int (*read)(struct upipe_http_src_hook *, int);
55+
int (*read)(struct upipe *upipe, struct upipe_http_src_hook *, int);
5656
/** called when the transport socket is ready for write */
57-
int (*write)(struct upipe_http_src_hook *, int);
57+
int (*write)(struct upipe *upipe, struct upipe_http_src_hook *, int);
5858
} transport;
5959
struct {
6060
/** called when there is data for read */
61-
ssize_t (*read)(struct upipe_http_src_hook *, uint8_t *, size_t);
61+
ssize_t (*read)(struct upipe *upipe, struct upipe_http_src_hook *,
62+
uint8_t *, size_t);
6263
/** called when there is space for data to write */
63-
ssize_t (*write)(struct upipe_http_src_hook *, const uint8_t *, size_t);
64+
ssize_t (*write)(struct upipe *upipe, struct upipe_http_src_hook *,
65+
const uint8_t *, size_t);
6466
} data;
6567
};
6668

lib/upipe-bearssl/https_source_hook_bearssl.c

Lines changed: 146 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,137 @@
3636

3737
#include "https_source_hook_bearssl.h"
3838

39+
static const char *error_string(int error_code)
40+
{
41+
switch (error_code) {
42+
case BR_ERR_OK:
43+
return "OK";
44+
case BR_ERR_BAD_PARAM:
45+
return "BAD_PARAM";
46+
case BR_ERR_BAD_STATE:
47+
return "BAD_STATE";
48+
case BR_ERR_UNSUPPORTED_VERSION:
49+
return "UNSUPPORTED_VERSION";
50+
case BR_ERR_BAD_VERSION:
51+
return "BAD_VERSION";
52+
case BR_ERR_BAD_LENGTH:
53+
return "BAD_LENGTH";
54+
case BR_ERR_TOO_LARGE:
55+
return "TOO_LARGE";
56+
case BR_ERR_BAD_MAC:
57+
return "BAD_MAC";
58+
case BR_ERR_NO_RANDOM:
59+
return "NO_RANDOM";
60+
case BR_ERR_UNKNOWN_TYPE:
61+
return "UNKNOWN_TYPE";
62+
case BR_ERR_UNEXPECTED:
63+
return "UNEXPECTED";
64+
case BR_ERR_BAD_CCS:
65+
return "BAD_CCS";
66+
case BR_ERR_BAD_ALERT:
67+
return "BAD_ALERT";
68+
case BR_ERR_BAD_HANDSHAKE:
69+
return "BAD_HANDSHAKE";
70+
case BR_ERR_OVERSIZED_ID:
71+
return "OVERSIZED_ID";
72+
case BR_ERR_BAD_CIPHER_SUITE:
73+
return "BAD_CIPHER_SUITE";
74+
case BR_ERR_BAD_COMPRESSION:
75+
return "BAD_COMPRESSION";
76+
case BR_ERR_BAD_FRAGLEN:
77+
return "BAD_FRAGLEN";
78+
case BR_ERR_BAD_SECRENEG:
79+
return "BAD_SECRENEG";
80+
case BR_ERR_EXTRA_EXTENSION:
81+
return "EXTRA_EXTENSION";
82+
case BR_ERR_BAD_SNI:
83+
return "BAD_SNI";
84+
case BR_ERR_BAD_HELLO_DONE:
85+
return "BAD_HELLO_DONE";
86+
case BR_ERR_LIMIT_EXCEEDED:
87+
return "LIMIT_EXCEEDED";
88+
case BR_ERR_BAD_FINISHED:
89+
return "BAD_FINISHED";
90+
case BR_ERR_RESUME_MISMATCH:
91+
return "RESUME_MISMATCH";
92+
case BR_ERR_INVALID_ALGORITHM:
93+
return "INVALID_ALGORITHM";
94+
case BR_ERR_BAD_SIGNATURE:
95+
return "BAD_SIGNATURE";
96+
case BR_ERR_WRONG_KEY_USAGE:
97+
return "WRONG_KEY_USAGE";
98+
case BR_ERR_NO_CLIENT_AUTH:
99+
return "NO_CLIENT_AUTH";
100+
case BR_ERR_IO:
101+
return "IO";
102+
case BR_ERR_RECV_FATAL_ALERT:
103+
return "RECV_FATAL_ALERT";
104+
case BR_ERR_SEND_FATAL_ALERT:
105+
return "SEND_FATAL_ALERT";
106+
case BR_ERR_X509_OK:
107+
return "X509_OK";
108+
case BR_ERR_X509_INVALID_VALUE:
109+
return "X509_INVALID_VALUE";
110+
case BR_ERR_X509_TRUNCATED:
111+
return "X509_TRUNCATED";
112+
case BR_ERR_X509_EMPTY_CHAIN:
113+
return "X509_EMPTY_CHAIN";
114+
case BR_ERR_X509_INNER_TRUNC:
115+
return "X509_INNER_TRUNC";
116+
case BR_ERR_X509_BAD_TAG_CLASS:
117+
return "X509_BAD_TAG_CLASS";
118+
case BR_ERR_X509_BAD_TAG_VALUE:
119+
return "X509_BAD_TAG_VALUE";
120+
case BR_ERR_X509_INDEFINITE_LENGTH:
121+
return "X509_INDEFINITE_LENGTH";
122+
case BR_ERR_X509_EXTRA_ELEMENT:
123+
return "X509_EXTRA_ELEMENT";
124+
case BR_ERR_X509_UNEXPECTED:
125+
return "X509_UNEXPECTED";
126+
case BR_ERR_X509_NOT_CONSTRUCTED:
127+
return "X509_NOT_CONSTRUCTED";
128+
case BR_ERR_X509_NOT_PRIMITIVE:
129+
return "X509_NOT_PRIMITIVE";
130+
case BR_ERR_X509_PARTIAL_BYTE:
131+
return "X509_PARTIAL_BYTE";
132+
case BR_ERR_X509_BAD_BOOLEAN:
133+
return "X509_BAD_BOOLEAN";
134+
case BR_ERR_X509_OVERFLOW:
135+
return "X509_OVERFLOW";
136+
case BR_ERR_X509_BAD_DN:
137+
return "X509_BAD_DN";
138+
case BR_ERR_X509_BAD_TIME:
139+
return "X509_BAD_TIME";
140+
case BR_ERR_X509_UNSUPPORTED:
141+
return "X509_UNSUPPORTED";
142+
case BR_ERR_X509_LIMIT_EXCEEDED:
143+
return "X509_LIMIT_EXCEEDED";
144+
case BR_ERR_X509_WRONG_KEY_TYPE:
145+
return "X509_WRONG_KEY_TYPE";
146+
case BR_ERR_X509_BAD_SIGNATURE:
147+
return "X509_BAD_SIGNATURE";
148+
case BR_ERR_X509_TIME_UNKNOWN:
149+
return "X509_TIME_UNKNOWN";
150+
case BR_ERR_X509_EXPIRED:
151+
return "X509_EXPIRED";
152+
case BR_ERR_X509_DN_MISMATCH:
153+
return "X509_DN_MISMATCH";
154+
case BR_ERR_X509_BAD_SERVER_NAME:
155+
return "X509_BAD_SERVER_NAME";
156+
case BR_ERR_X509_CRITICAL_EXTENSION:
157+
return "X509_CRITICAL_EXTENSION";
158+
case BR_ERR_X509_NOT_CA:
159+
return "X509_NOT_CA";
160+
case BR_ERR_X509_FORBIDDEN_KEY_USAGE:
161+
return "X509_FORBIDDEN_KEY_USAGE";
162+
case BR_ERR_X509_WEAK_PUBLIC_KEY:
163+
return "X509_WEAK_PUBLIC_KEY";
164+
case BR_ERR_X509_NOT_TRUSTED:
165+
return "X509_NOT_TRUSTED";
166+
}
167+
return "Unknown error";
168+
}
169+
39170
/** This describes a x509 no anchor context to allow not trusted certificate. */
40171
struct x509_noanchor_context {
41172
const br_x509_class *vtable;
@@ -168,12 +299,14 @@ static int https_src_hook_state_to_code(unsigned state)
168299

169300
/** @internal @This reads from the socket to the SSL engine.
170301
*
302+
* @param upipe description structure of the pipe
171303
* @param hook SSL hook structure
172304
* @param fd socket file descriptor
173305
* @return 0 or negative value on error, 1 if more data is needed, 2 otherwise
174306
*/
175307
static int
176-
https_src_hook_transport_read(struct upipe_http_src_hook *hook, int fd)
308+
https_src_hook_transport_read(struct upipe *upipe,
309+
struct upipe_http_src_hook *hook, int fd)
177310
{
178311
struct https_src_hook_bearssl *https =
179312
https_src_hook_bearssl_from_hook(hook);
@@ -196,12 +329,14 @@ https_src_hook_transport_read(struct upipe_http_src_hook *hook, int fd)
196329

197330
/** @internal @This writes from the SSL engine to the socket.
198331
*
332+
* @param upipe description structure of the pipe
199333
* @param hook SSL hook structure
200334
* @param fd socket file descriptor
201335
* @return 0 or negative value on error, 1 if more data is needed, 2 otherwise
202336
*/
203337
static int
204-
https_src_hook_transport_write(struct upipe_http_src_hook *hook, int fd)
338+
https_src_hook_transport_write(struct upipe *upipe,
339+
struct upipe_http_src_hook *hook, int fd)
205340
{
206341
struct https_src_hook_bearssl *https =
207342
https_src_hook_bearssl_from_hook(hook);
@@ -223,13 +358,15 @@ https_src_hook_transport_write(struct upipe_http_src_hook *hook, int fd)
223358

224359
/** @internal @This reads data from the SSL engine to a buffer.
225360
*
361+
* @param upipe description structure of the pipe
226362
* @param hook SSL hook structure
227363
* @param buffer filled with data
228364
* @param count buffer size
229365
* @return a negative value on error, 0 if the connection is closed, the number
230366
* of bytes written to the buffer
231367
*/
232-
static ssize_t https_src_hook_data_read(struct upipe_http_src_hook *hook,
368+
static ssize_t https_src_hook_data_read(struct upipe *upipe,
369+
struct upipe_http_src_hook *hook,
233370
uint8_t *buffer, size_t count)
234371
{
235372
struct https_src_hook_bearssl *https =
@@ -247,8 +384,10 @@ static ssize_t https_src_hook_data_read(struct upipe_http_src_hook *hook,
247384
}
248385
else if (state & BR_SSL_CLOSED) {
249386
int err = br_ssl_engine_last_error(eng);
250-
if (err)
387+
if (err) {
388+
upipe_err_va(upipe, "connection failed (%s)", error_string(err));
251389
errno = EIO;
390+
}
252391
rsize = err ? -1 : 0;
253392
}
254393
else
@@ -259,12 +398,14 @@ static ssize_t https_src_hook_data_read(struct upipe_http_src_hook *hook,
259398

260399
/** @internal @This writes data from a buffer to the SSL engine.
261400
*
401+
* @param upipe description structure of the pipe
262402
* @param hook SSL hook structure
263403
* @param buffer data to write
264404
* @param count buffer number of bytes in the buffer
265405
* @return a negative value on error or the number of bytes read from the buffer
266406
*/
267-
static ssize_t https_src_hook_data_write(struct upipe_http_src_hook *hook,
407+
static ssize_t https_src_hook_data_write(struct upipe *upipe,
408+
struct upipe_http_src_hook *hook,
268409
const uint8_t *buffer, size_t count)
269410
{
270411
struct https_src_hook_bearssl *https =

lib/upipe-modules/http_source_hook.c

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* @short HTTP hooks for plain data read/write.
2828
*/
2929

30+
#include <string.h>
3031
#include <errno.h>
3132

3233
#include "upipe/ubase.h"
@@ -52,21 +53,25 @@ static int http_src_hook_state(struct http_src_hook *http)
5253

5354
/** @internal @This reads from the socket to the plain engine.
5455
*
56+
* @param upipe description structure of the pipe
5557
* @param hook plain hook structure
5658
* @param fd socket file descriptor
5759
* @return 0 or negative value on error, 1 if more data is needed, 2 otherwise
5860
*/
5961
static int
60-
http_src_hook_transport_read(struct upipe_http_src_hook *hook, int fd)
62+
http_src_hook_transport_read(struct upipe *upipe,
63+
struct upipe_http_src_hook *hook, int fd)
6164
{
6265
struct http_src_hook *http = http_src_hook_from_hook(hook);
6366
size_t size = UPIPE_HTTP_SRC_HOOK_BUFFER - http->out.len;
6467
if (size) {
6568
ssize_t rsize = read(fd, http->out.buf + http->out.len, size);
66-
if (rsize <= 0) {
67-
if (rsize == 0)
68-
http->closed = true;
69-
return rsize;
69+
if (rsize < 0) {
70+
upipe_err_va(upipe, "read error (%s)", strerror(errno));
71+
return -1;
72+
} else if (rsize == 0) {
73+
http->closed = true;
74+
return 0;
7075
}
7176
http->out.len += rsize;
7277
}
@@ -75,20 +80,27 @@ http_src_hook_transport_read(struct upipe_http_src_hook *hook, int fd)
7580

7681
/** @internal @This writes from the plain engine to the socket.
7782
*
83+
* @param upipe description structure of the pipe
7884
* @param hook plain hook structure
7985
* @param fd socket file descriptor
8086
* @return 0 or negative value on error, 1 if more data is needed, 2 otherwise
8187
*/
8288
static int
83-
http_src_hook_transport_write(struct upipe_http_src_hook *hook, int fd)
89+
http_src_hook_transport_write(struct upipe *upipe,
90+
struct upipe_http_src_hook *hook, int fd)
8491
{
8592
struct http_src_hook *http = http_src_hook_from_hook(hook);
8693
size_t size = http->in.len;
8794
ssize_t wsize = -1;
8895
if (size) {
8996
wsize = write(fd, http->in.buf, size);
90-
if (wsize <= 0)
91-
return wsize;
97+
if (wsize < 0) {
98+
upipe_err_va(upipe, "write error (%s)", strerror(errno));
99+
return -1;
100+
} else if (wsize == 0) {
101+
http->closed = true;
102+
return 0;
103+
}
92104
memmove(http->in.buf, http->in.buf + wsize, size - wsize);
93105
http->in.len -= wsize;
94106
}
@@ -98,15 +110,16 @@ http_src_hook_transport_write(struct upipe_http_src_hook *hook, int fd)
98110

99111
/** @internal @This reads data from the plain engine to a buffer.
100112
*
113+
* @param upipe description structure of the pipe
101114
* @param hook plain hook structure
102115
* @param buffer filled with data
103116
* @param count buffer size
104117
* @return a negative value on error, 0 if the connection is closed, the number
105118
* of bytes written to the buffer
106119
*/
107-
static ssize_t
108-
http_src_hook_data_read(struct upipe_http_src_hook *hook,
109-
uint8_t *buffer, size_t count)
120+
static ssize_t http_src_hook_data_read(struct upipe *upipe,
121+
struct upipe_http_src_hook *hook,
122+
uint8_t *buffer, size_t count)
110123
{
111124
struct http_src_hook *http = http_src_hook_from_hook(hook);
112125
size_t size = count > http->out.len ? http->out.len : count;
@@ -124,14 +137,15 @@ http_src_hook_data_read(struct upipe_http_src_hook *hook,
124137

125138
/** @internal @This writes data from a buffer to the plain engine.
126139
*
140+
* @param upipe description structure of the pipe
127141
* @param hook plain hook structure
128142
* @param buffer data to write
129143
* @param count buffer number of bytes in the buffer
130144
* @return a negative value on error or the number of bytes read from the buffer
131145
*/
132-
static ssize_t
133-
http_src_hook_data_write(struct upipe_http_src_hook *hook,
134-
const uint8_t *buffer, size_t count)
146+
static ssize_t http_src_hook_data_write(struct upipe *upipe,
147+
struct upipe_http_src_hook *hook,
148+
const uint8_t *buffer, size_t count)
135149
{
136150
struct http_src_hook *http = http_src_hook_from_hook(hook);
137151
size_t size = UPIPE_HTTP_SRC_HOOK_BUFFER - http->in.len;

lib/upipe-modules/upipe_http_source.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -794,8 +794,8 @@ static void upipe_http_src_worker_read(struct upump *upump)
794794
if (likely(upipe_http_src->upump_timeout))
795795
upump_restart(upipe_http_src->upump_timeout);
796796

797-
int ret = upipe_http_src->hook->transport.read(upipe_http_src->hook,
798-
upipe_http_src->fd);
797+
int ret = upipe_http_src->hook->transport.read(
798+
upipe, upipe_http_src->hook, upipe_http_src->fd);
799799
upipe_http_src_worker_update_state(upipe, ret);
800800
}
801801

@@ -807,8 +807,8 @@ static void upipe_http_src_worker_write(struct upump *upump)
807807
if (likely(upipe_http_src->upump_timeout))
808808
upump_restart(upipe_http_src->upump_timeout);
809809

810-
int ret = upipe_http_src->hook->transport.write(upipe_http_src->hook,
811-
upipe_http_src->fd);
810+
int ret = upipe_http_src->hook->transport.write(
811+
upipe, upipe_http_src->hook, upipe_http_src->fd);
812812
upipe_http_src_worker_update_state(upipe, ret);
813813
}
814814

@@ -841,7 +841,8 @@ static void upipe_http_src_data_in(struct upump *upump)
841841
return;
842842

843843
int ret = upipe_http_src->hook->data.write(
844-
upipe_http_src->hook, (unsigned char *)request->buf, request->len);
844+
upipe, upipe_http_src->hook, (unsigned char *)request->buf,
845+
request->len);
845846

846847
if (ret < 0) {
847848
switch(errno) {
@@ -897,7 +898,7 @@ static void upipe_http_src_data_out(struct upump *upump)
897898
uint8_t buffer[upipe_http_src->output_size];
898899
ssize_t len =
899900
upipe_http_src->hook->data.read(
900-
upipe_http_src->hook, buffer, upipe_http_src->output_size);
901+
upipe, upipe_http_src->hook, buffer, upipe_http_src->output_size);
901902
if (unlikely(len < 0)) {
902903
switch (errno) {
903904
case EINTR:

0 commit comments

Comments
 (0)