inputStream_http: cleanup GET request code (finishHTTPInit)

Avoid unnecessary memset to zero, snprintf always puts a
trailing '\0'.  We also have no need to subtract one from the
buffer we're snprintf-ing it to.

We also check the return value of snprintf to ensure it's not
too long.  I have a feeling we might as well avoid snprintf
altogether so we don't have to worry about buffer sizing/stack
overflow and just do a bunch of write(2)s, letting Nagle sort it
out...

Also, centralize some of the exit error handling in with
goto.  This makes the code a bit more consistent and
maintainable as well as reducing code and binary size.

git-svn-id: https://svn.musicpd.org/mpd/trunk@5395 09075e82-0dd4-0310-85a5-a0d7c8717e4f
This commit is contained in:
Eric Wong 2007-02-19 07:58:03 +00:00
parent 9372692127
commit 772dc6bc66

View File

@ -446,7 +446,8 @@ static int finishHTTPInit(InputStream * inStream)
int error; int error;
socklen_t error_len = sizeof(int); socklen_t error_len = sizeof(int);
int ret; int ret;
char request[2049]; int length;
char request[2048];
tv.tv_sec = 0; tv.tv_sec = 0;
tv.tv_usec = 0; tv.tv_usec = 0;
@ -463,44 +464,43 @@ static int finishHTTPInit(InputStream * inStream)
if (ret < 0) { if (ret < 0) {
DEBUG(__FILE__ ": problem select'ing: %s\n", strerror(errno)); DEBUG(__FILE__ ": problem select'ing: %s\n", strerror(errno));
close(data->sock); goto close_err;
data->connState = HTTP_CONN_STATE_CLOSED;
return -1;
} }
getsockopt(data->sock, SOL_SOCKET, SO_ERROR, &error, &error_len); getsockopt(data->sock, SOL_SOCKET, SO_ERROR, &error, &error_len);
if (error) { if (error)
close(data->sock); goto close_err;
data->connState = HTTP_CONN_STATE_CLOSED;
return -1;
}
memset(request, 0, 2049);
/* deal with ICY metadata later, for now its fucking up stuff! */ /* deal with ICY metadata later, for now its fucking up stuff! */
snprintf(request, 2048, "GET %s HTTP/1.1\r\n" "Host: %s\r\n" length = snprintf(request, sizeof(request),
"GET %s HTTP/1.1\r\n" "Host: %s\r\n"
/*"Connection: close\r\n" */ /*"Connection: close\r\n" */
"User-Agent: %s/%s\r\n" "User-Agent: %s/%s\r\n"
"Range: bytes=%ld-\r\n" "Range: bytes=%ld-\r\n"
"%s" /* authorization */ "%s" /* authorization */
"Icy-Metadata:1\r\n" "Icy-Metadata:1\r\n"
"\r\n", data->path, data->host, PACKAGE_NAME, PACKAGE_VERSION, "\r\n",
data->path, data->host,
PACKAGE_NAME, PACKAGE_VERSION,
inStream->offset, inStream->offset,
data->proxyAuth ? data->proxyAuth : data->proxyAuth ? data->proxyAuth :
(data->httpAuth ? data->httpAuth : "") (data->httpAuth ? data->httpAuth : ""));
);
ret = write(data->sock, request, strlen(request)); if (length >= sizeof(request))
if (ret != strlen(request)) { goto close_err;
ret = write(data->sock, request, length);
if (ret != length)
goto close_err;
data->connState = HTTP_CONN_STATE_HELLO;
return 0;
close_err:
close(data->sock); close(data->sock);
data->connState = HTTP_CONN_STATE_CLOSED; data->connState = HTTP_CONN_STATE_CLOSED;
return -1; return -1;
} }
data->connState = HTTP_CONN_STATE_HELLO;
return 0;
}
static int getHTTPHello(InputStream * inStream) static int getHTTPHello(InputStream * inStream)
{ {
InputStreamHTTPData *data = (InputStreamHTTPData *) inStream->data; InputStreamHTTPData *data = (InputStreamHTTPData *) inStream->data;