diff --git a/include/mosquitto.h b/include/mosquitto.h index 6c785bbb..840e9da5 100644 --- a/include/mosquitto.h +++ b/include/mosquitto.h @@ -1597,7 +1597,10 @@ libmosq_EXPORT int mosquitto_opts_set(struct mosquitto *mosq, enum mosq_opt_t op * * MOSQ_OPT_HTTP_HEADER_SIZE - Size the size of buffer that will be allocated * to store the incoming HTTP header when using Websocket transport. - * Defaults to 4096. + * Defaults to 4096. Setting to below 100 will result in a return +* value of MOSQ_ERR_INVAL. This should be set before starting the +* connection. If you try to set this when the initial http request +* is underway then it will return MOSQ_ERR_INVAL. */ libmosq_EXPORT int mosquitto_int_option(struct mosquitto *mosq, enum mosq_opt_t option, int value); diff --git a/lib/http_client.c b/lib/http_client.c index f563eff3..69079924 100644 --- a/lib/http_client.c +++ b/lib/http_client.c @@ -51,7 +51,7 @@ int http_c__context_init(struct mosquitto *context) const char *path; context->transport = mosq_t_http; - context->http_request = mosquitto__calloc(1, 4096); // FIXME - 4096 should be an option + context->http_request = mosquitto__calloc(1, (size_t)context->wsd.http_header_size); if(context->http_request == NULL){ return MOSQ_ERR_NOMEM; } @@ -122,7 +122,7 @@ int http_c__read(struct mosquitto *mosq) } hlen = strlen(mosq->http_request); - read_length = net__read(mosq, &mosq->http_request[hlen], 4096-hlen); + read_length = net__read(mosq, &mosq->http_request[hlen], (size_t)mosq->wsd.http_header_size-hlen); if(read_length <= 0){ if(read_length == 0){ return MOSQ_ERR_CONN_LOST; /* EOF */ diff --git a/lib/mosquitto.c b/lib/mosquitto.c index fed6e6cd..e24a6916 100644 --- a/lib/mosquitto.c +++ b/lib/mosquitto.c @@ -162,6 +162,7 @@ int mosquitto_reinitialise(struct mosquitto *mosq, const char *id, bool clean_st mosq->wsd.mask = UINT8_MAX; mosq->wsd.disconnect_reason = 0xE8; mosq->wsd.is_client = true; + mosq->wsd.http_header_size = 4096; #endif mosq->transport = mosq_t_tcp; mosq->protocol = mosq_p_mqtt311; diff --git a/lib/options.c b/lib/options.c index 53f949cc..bbf190ff 100644 --- a/lib/options.c +++ b/lib/options.c @@ -517,6 +517,12 @@ int mosquitto_int_option(struct mosquitto *mosq, enum mosq_opt_t option, int val case MOSQ_OPT_HTTP_HEADER_SIZE: #if defined(WITH_WEBSOCKETS) && WITH_WEBSOCKETS == LWS_IS_BUILTIN + if(value < 100){ /* arbitrary limit */ + return MOSQ_ERR_INVAL; + }else if(mosq->http_request){ + /* Don't want to resize if part way through the handshake */ + return MOSQ_ERR_INVAL; + } mosq->wsd.http_header_size = value; #else return MOSQ_ERR_NOT_SUPPORTED;