Skip to content

Commit f89856b

Browse files
committed
Make HTTP_MAX_BUFFER and HTTP_MAX_VALUE private constants.
Increase HTTP_MAX_BUFFER to 32k. Update default non-blocking timeout to 1 second, and allow httpSetTimeout to control it. Mirror timeout support in OpenSSL read code from GNU TLS support code. Fix clang-detected warnings.
1 parent e15e3b4 commit f89856b

11 files changed

Lines changed: 72 additions & 38 deletions

File tree

cups/auth.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ cupsDoAuthentication(
102102
if (cg->oauth_cb)
103103
{
104104
// Try callback...
105-
char scope[HTTP_MAX_VALUE]; // scope="xyz" string
105+
char scope[_HTTP_MAX_VALUE]; // scope="xyz" string
106106

107107
cups_auth_param(schemedata, "realm", http->realm, sizeof(http->realm));
108108

@@ -136,7 +136,7 @@ cupsDoAuthentication(
136136
if (http->digest_tries > 1 || !http->userpass[0])
137137
{
138138
// Nope - get a new password from the user...
139-
char default_username[HTTP_MAX_VALUE];
139+
char default_username[_HTTP_MAX_VALUE];
140140
// Default username
141141

142142
if (!cg->lang_default)
@@ -186,7 +186,7 @@ cupsDoAuthentication(
186186
else if (!_cups_strcasecmp(scheme, "Digest"))
187187
{
188188
// Digest authentication...
189-
char nonce[HTTP_MAX_VALUE]; // nonce="xyz" string
189+
char nonce[_HTTP_MAX_VALUE]; // nonce="xyz" string
190190

191191
cups_auth_param(schemedata, "algorithm", http->algorithm, sizeof(http->algorithm));
192192
cups_auth_param(schemedata, "nonce", nonce, sizeof(nonce));

cups/getputfile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ cupsGetFd(http_t *http, // I - Connection to server or @code CUPS_HTTP_DEFA
3333
ssize_t bytes; // Number of bytes read
3434
char buffer[8192]; // Buffer for file
3535
http_status_t status; // HTTP status from server
36-
char if_modified_since[HTTP_MAX_VALUE];
36+
char if_modified_since[_HTTP_MAX_VALUE];
3737
// If-Modified-Since header
3838
int new_auth = 0; // Using new auth information?
3939
int digest; // Are we using Digest authentication?

cups/http-private.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ extern "C" {
5050
// Constants...
5151
//
5252

53+
# define _HTTP_MAX_BUFFER 32768 // Max length of data buffer
5354
# define _HTTP_MAX_SBUFFER 65536 // Size of (de)compression buffer
55+
# define _HTTP_MAX_VALUE 256 // Max header field value length
5456

5557
# define _HTTP_TLS_NONE 0 // No TLS options
5658
# define _HTTP_TLS_ALLOW_RC4 1 // Allow RC4 cipher suites
@@ -127,31 +129,31 @@ struct _http_s // HTTP connection structure
127129
http_keepalive_t keep_alive; // Keep-alive supported?
128130
unsigned nonce_count; // Nonce count
129131
int digest_tries; // Number of tries for digest auth
130-
char userpass[HTTP_MAX_VALUE];
132+
char userpass[_HTTP_MAX_VALUE];
131133
// Username:password string
132134
char *data; // Pointer to data buffer
133135
http_encoding_t data_encoding; // Chunked or not
134136
off_t data_remaining; // Number of bytes left
135137
int used; // Number of bytes used in buffer
136-
char buffer[HTTP_MAX_BUFFER];
138+
char buffer[_HTTP_MAX_BUFFER];
137139
// Buffer for incoming data
138140
char algorithm[65], // Algorithm from WWW-Authenticate
139-
nextnonce[HTTP_MAX_VALUE],
141+
nextnonce[_HTTP_MAX_VALUE],
140142
// Next nonce value from Authentication-Info
141-
nonce[HTTP_MAX_VALUE],
143+
nonce[_HTTP_MAX_VALUE],
142144
// Nonce value
143-
opaque[HTTP_MAX_VALUE],
145+
opaque[_HTTP_MAX_VALUE],
144146
// Opaque value from WWW-Authenticate
145-
qop[HTTP_MAX_VALUE],
147+
qop[_HTTP_MAX_VALUE],
146148
// Quality of Protection (qop) value from WWW-Authenticate
147-
realm[HTTP_MAX_VALUE];
149+
realm[_HTTP_MAX_VALUE];
148150
// Realm from WWW-Authenticate
149151
http_encryption_t encryption; // Encryption requirements
150152
_http_tls_t tls; // TLS state information
151153
_http_tls_credentials_t *tls_credentials;
152154
// TLS credentials
153155
bool tls_upgrade; // `true` if we are doing an upgrade
154-
char wbuffer[HTTP_MAX_BUFFER];
156+
char wbuffer[_HTTP_MAX_BUFFER];
155157
// Buffer for outgoing data
156158
int wused; // Write buffer bytes used
157159
// TLS credentials

cups/http-support.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ _httpSetDigestAuthString(
993993
char kd[65], // Final MD5/SHA-256 digest
994994
ha1[65], // Hash of username:realm:password
995995
ha2[65], // Hash of method:request-uri
996-
username[HTTP_MAX_VALUE],
996+
username[_HTTP_MAX_VALUE],
997997
// username:password
998998
*password, // Pointer to password
999999
temp[1024], // Temporary string

cups/http.c

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ httpGetContentEncoding(http_t *http) // I - HTTP connection
778778
if (http && http->fields[HTTP_FIELD_ACCEPT_ENCODING])
779779
{
780780
int i; // Looping var
781-
char temp[HTTP_MAX_VALUE], // Copy of Accepts-Encoding value
781+
char temp[_HTTP_MAX_VALUE], // Copy of Accepts-Encoding value
782782
*start, // Start of coding value
783783
*end; // End of coding value
784784
double qvalue; // "qvalue" for coding
@@ -1190,7 +1190,7 @@ httpGets(http_t *http, // I - HTTP connection
11901190
return (NULL);
11911191
}
11921192

1193-
bytes = http_read(http, http->buffer + http->used, (size_t)(HTTP_MAX_BUFFER - http->used));
1193+
bytes = http_read(http, http->buffer + http->used, (size_t)(_HTTP_MAX_BUFFER - http->used));
11941194

11951195
DEBUG_printf("4httpGets: read " CUPS_LLFMT " bytes.", CUPS_LLCAST bytes);
11961196

@@ -1331,7 +1331,7 @@ httpGetSubField(http_t *http, // I - HTTP connection
13311331
size_t valuelen) // I - Size of value buffer
13321332
{
13331333
const char *fptr; // Pointer into field
1334-
char temp[HTTP_MAX_VALUE], // Temporary buffer for name
1334+
char temp[_HTTP_MAX_VALUE], // Temporary buffer for name
13351335
*ptr, // Pointer into string buffer
13361336
*end; // End of value buffer
13371337

@@ -1629,9 +1629,9 @@ httpPeek(http_t *http, // I - HTTP connection
16291629

16301630
memset(&stream, 0, sizeof(stream));
16311631

1632-
if (http->used > 0 && ((z_stream *)http->stream)->avail_in < HTTP_MAX_BUFFER)
1632+
if (http->used > 0 && ((z_stream *)http->stream)->avail_in < _HTTP_MAX_BUFFER)
16331633
{
1634-
size_t buflen = HTTP_MAX_BUFFER - ((z_stream *)http->stream)->avail_in;
1634+
size_t buflen = _HTTP_MAX_BUFFER - ((z_stream *)http->stream)->avail_in;
16351635
// Number of bytes to copy
16361636

16371637
if (((z_stream *)http->stream)->avail_in > 0 && ((z_stream *)http->stream)->next_in > http->sbuffer)
@@ -1830,7 +1830,7 @@ httpRead(http_t *http, // I - HTTP connection
18301830

18311831
if (bytes == 0)
18321832
{
1833-
ssize_t buflen = HTTP_MAX_BUFFER - (ssize_t)((z_stream *)http->stream)->avail_in;
1833+
ssize_t buflen = _HTTP_MAX_BUFFER - (ssize_t)((z_stream *)http->stream)->avail_in;
18341834
// Additional bytes for buffer
18351835

18361836
if (buflen > 0)
@@ -2464,14 +2464,38 @@ bool // O - `true` to continue, `false` to stop
24642464
_httpUpdate(http_t *http, // I - HTTP connection
24652465
http_status_t *status) // O - Current HTTP status
24662466
{
2467-
char line[32768], // Line from connection...
2467+
char line[_HTTP_MAX_BUFFER], // Line from connection...
24682468
*value; // Pointer to value on line
24692469
http_field_t field; // Field index
24702470
int major, minor; // HTTP version numbers
24712471

24722472

24732473
DEBUG_printf("_httpUpdate(http=%p, status=%p), state=%s", (void *)http, (void *)status, httpStateString(http->state));
24742474

2475+
// When doing non-blocking I/O, make sure we have a whole line...
2476+
if (!http->blocking)
2477+
{
2478+
ssize_t bytes; // Bytes "peeked" from connection
2479+
2480+
// Peek at the incoming data...
2481+
if ((bytes = httpPeek(http, line, sizeof(line) - 1)) < 0)
2482+
{
2483+
// Unable to peek, return an error...
2484+
*status = HTTP_STATUS_ERROR;
2485+
return (0);
2486+
}
2487+
2488+
// Nul-terminate the data and see if we have a newline...
2489+
line[bytes] = '\0';
2490+
2491+
if (!strchr(line, '\n'))
2492+
{
2493+
// Don't have a full line, tell the reader to try again when there is more data...
2494+
*status = HTTP_STATUS_CONTINUE;
2495+
return (0);
2496+
}
2497+
}
2498+
24752499
// Grab a single line from the connection...
24762500
if (!httpGets(http, line, sizeof(line)))
24772501
{
@@ -4088,15 +4112,15 @@ http_set_timeout(int fd, // I - File descriptor
40884112
static void
40894113
http_set_wait(http_t *http) // I - HTTP connection
40904114
{
4091-
if (http->blocking)
4092-
{
4093-
http->wait_value = (int)(http->timeout_value * 1000);
4115+
http->wait_value = (int)(http->timeout_value * 1000);
40944116

4095-
if (http->wait_value <= 0)
4117+
if (http->wait_value <= 0)
4118+
{
4119+
if (http->blocking)
40964120
http->wait_value = 60000;
4121+
else
4122+
http->wait_value = 1000;
40974123
}
4098-
else
4099-
http->wait_value = 10000;
41004124
}
41014125

41024126

cups/http.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ extern "C" {
7777

7878
# define HTTP_MAX_URI 1024 // Max length of URI string
7979
# define HTTP_MAX_HOST 256 // Max length of hostname string
80-
# define HTTP_MAX_BUFFER 2048 // Max length of data buffer
81-
# define HTTP_MAX_VALUE 256 // Max header field value length
8280

8381

8482
//

cups/json.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ cupsJSONImportURL(
11291129
char resource[1024]; // URL resource path
11301130
http_status_t status; // HTTP request status
11311131
http_state_t initial_state; // Initial HTTP state
1132-
char if_modified_since[HTTP_MAX_VALUE];
1132+
char if_modified_since[_HTTP_MAX_VALUE];
11331133
// If-Modified-Since header
11341134
bool new_auth = false, // Using new auth information?
11351135
digest; // Are we using Digest authentication?

cups/request.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ cupsWriteRequestData(
752752
}
753753

754754
// Finally, check if we have any pending data from the server...
755-
if (length >= HTTP_MAX_BUFFER || http->wused < wused || (wused > 0 && (size_t)http->wused == length))
755+
if (length >= _HTTP_MAX_BUFFER || http->wused < wused || (wused > 0 && (size_t)http->wused == length))
756756
{
757757
// We've written something to the server, so check for response data...
758758
if (_httpWait(http, 0, 1))

cups/tls-openssl.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,11 +2182,14 @@ http_bio_read(BIO *h, // I - BIO data
21822182
http = (http_t *)BIO_get_data(h);
21832183
DEBUG_printf("9http_bio_read: http=%p", (void *)http);
21842184

2185-
if (!http->blocking)
2185+
if (!http->blocking || http->timeout_value > 0.0)
21862186
{
21872187
// Make sure we have data before we read...
2188-
if (!_httpWait(http, 10000, 0))
2188+
while (!_httpWait(http, http->wait_value, 0))
21892189
{
2190+
if (http->timeout_cb && (*http->timeout_cb)(http, http->timeout_data))
2191+
continue;
2192+
21902193
#ifdef WIN32
21912194
http->error = WSAETIMEDOUT;
21922195
#else

tools/ippeveprinter.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static const char * const ippeve_preason_strings[] =
136136
#if HAVE_LIBPAM
137137
typedef struct ippeve_authdata_s // Authentication data
138138
{
139-
char username[HTTP_MAX_VALUE], // Username string
139+
char username[256], // Username string
140140
*password; // Password string
141141
} ippeve_authdata_t;
142142
#endif // HAVE_LIBPAM
@@ -213,13 +213,11 @@ typedef struct ippeve_client_s // Client data
213213
ipp_op_t operation_id; // IPP operation-id
214214
char uri[1024], // Request URI
215215
*options, // URI options
216-
host_field[HTTP_MAX_VALUE];
217-
// Host: header
216+
host_field[256];// Host: header
218217
int host_port; // Port number from Host: header
219218
http_addr_t addr; // Client address
220219
char hostname[256], // Client hostname
221-
username[HTTP_MAX_VALUE];
222-
// Authenticated username, if any
220+
username[256]; // Authenticated username, if any
223221
ippeve_printer_t *printer; // Printer
224222
ippeve_job_t *job; // Current job, if any
225223
} ippeve_client_t;

0 commit comments

Comments
 (0)