Use htons and ntohs in fastcgi
This commit is contained in:
@ -79,15 +79,13 @@ int fastcgi_init(fastcgi_cnx_t *conn, int mode, unsigned int req_num, const sock
|
|||||||
|
|
||||||
FCGI_Header header = {
|
FCGI_Header header = {
|
||||||
.version = FCGI_VERSION_1,
|
.version = FCGI_VERSION_1,
|
||||||
.requestIdB1 = conn->req_id >> 8,
|
.requestId = htons(conn->req_id),
|
||||||
.requestIdB0 = conn->req_id & 0xFF,
|
|
||||||
.paddingLength = 0,
|
.paddingLength = 0,
|
||||||
.reserved = 0
|
.reserved = 0
|
||||||
};
|
};
|
||||||
|
|
||||||
header.type = FCGI_BEGIN_REQUEST;
|
header.type = FCGI_BEGIN_REQUEST;
|
||||||
header.contentLengthB1 = 0;
|
header.contentLength = htons(sizeof(FCGI_BeginRequestBody));
|
||||||
header.contentLengthB0 = sizeof(FCGI_BeginRequestBody);
|
|
||||||
FCGI_BeginRequestRecord begin = {
|
FCGI_BeginRequestRecord begin = {
|
||||||
header,
|
header,
|
||||||
{.roleB1 = (FCGI_RESPONDER >> 8) & 0xFF, .roleB0 = FCGI_RESPONDER & 0xFF, .flags = 0}
|
{.roleB1 = (FCGI_RESPONDER >> 8) & 0xFF, .roleB0 = FCGI_RESPONDER & 0xFF, .flags = 0}
|
||||||
@ -174,8 +172,7 @@ int fastcgi_init(fastcgi_cnx_t *conn, int mode, unsigned int req_num, const sock
|
|||||||
|
|
||||||
unsigned short param_len = param_ptr - param_buf - sizeof(header);
|
unsigned short param_len = param_ptr - param_buf - sizeof(header);
|
||||||
header.type = FCGI_PARAMS;
|
header.type = FCGI_PARAMS;
|
||||||
header.contentLengthB1 = param_len >> 8;
|
header.contentLength = htons(param_len);
|
||||||
header.contentLengthB0 = param_len & 0xFF;
|
|
||||||
memcpy(param_buf, &header, sizeof(header));
|
memcpy(param_buf, &header, sizeof(header));
|
||||||
if (send(conn->socket, param_buf, param_len + sizeof(header), 0) != param_len + sizeof(header)) {
|
if (send(conn->socket, param_buf, param_len + sizeof(header), 0) != param_len + sizeof(header)) {
|
||||||
error("Unable to send to FastCGI socket");
|
error("Unable to send to FastCGI socket");
|
||||||
@ -183,8 +180,7 @@ int fastcgi_init(fastcgi_cnx_t *conn, int mode, unsigned int req_num, const sock
|
|||||||
}
|
}
|
||||||
|
|
||||||
header.type = FCGI_PARAMS;
|
header.type = FCGI_PARAMS;
|
||||||
header.contentLengthB1 = 0;
|
header.contentLength = htons(0);
|
||||||
header.contentLengthB0 = 0;
|
|
||||||
if (send(conn->socket, &header, sizeof(header), 0) != sizeof(header)) {
|
if (send(conn->socket, &header, sizeof(header), 0) != sizeof(header)) {
|
||||||
error("Unable to send to FastCGI socket");
|
error("Unable to send to FastCGI socket");
|
||||||
return -2;
|
return -2;
|
||||||
@ -197,10 +193,8 @@ int fastcgi_close_stdin(fastcgi_cnx_t *conn) {
|
|||||||
FCGI_Header header = {
|
FCGI_Header header = {
|
||||||
.version = FCGI_VERSION_1,
|
.version = FCGI_VERSION_1,
|
||||||
.type = FCGI_STDIN,
|
.type = FCGI_STDIN,
|
||||||
.requestIdB1 = conn->req_id >> 8,
|
.requestId = htons(conn->req_id),
|
||||||
.requestIdB0 = conn->req_id & 0xFF,
|
.contentLength = htons(0),
|
||||||
.contentLengthB1 = 0,
|
|
||||||
.contentLengthB0 = 0,
|
|
||||||
.paddingLength = 0,
|
.paddingLength = 0,
|
||||||
.reserved = 0
|
.reserved = 0
|
||||||
};
|
};
|
||||||
@ -295,8 +289,8 @@ int fastcgi_header(fastcgi_cnx_t *conn, http_res *res, char *err_msg) {
|
|||||||
error("Unable to receive from FastCGI socket");
|
error("Unable to receive from FastCGI socket");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
req_id = (header.requestIdB1 << 8) | header.requestIdB0;
|
req_id = ntohs(header.requestId);
|
||||||
content_len = (header.contentLengthB1 << 8) | header.contentLengthB0;
|
content_len = ntohs(header.contentLength);
|
||||||
content = malloc(content_len + header.paddingLength);
|
content = malloc(content_len + header.paddingLength);
|
||||||
ret = recv(conn->socket, content, content_len + header.paddingLength, 0);
|
ret = recv(conn->socket, content, content_len + header.paddingLength, 0);
|
||||||
if (ret != (content_len + header.paddingLength)) {
|
if (ret != (content_len + header.paddingLength)) {
|
||||||
@ -404,8 +398,8 @@ int fastcgi_send(fastcgi_cnx_t *conn, sock *client, int flags) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
req_id = (header.requestIdB1 << 8) | header.requestIdB0;
|
req_id = ntohs(header.requestId);
|
||||||
content_len = (header.contentLengthB1 << 8) | header.contentLengthB0;
|
content_len = ntohs(header.contentLength);
|
||||||
content = malloc(content_len + header.paddingLength);
|
content = malloc(content_len + header.paddingLength);
|
||||||
ptr = content;
|
ptr = content;
|
||||||
|
|
||||||
@ -479,8 +473,8 @@ int fastcgi_dump(fastcgi_cnx_t *conn, char *buf, long len) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
req_id = (header.requestIdB1 << 8) | header.requestIdB0;
|
req_id = ntohs(header.requestId);
|
||||||
content_len = (header.contentLengthB1 << 8) | header.contentLengthB0;
|
content_len = ntohs(header.contentLength);
|
||||||
content = malloc(content_len + header.paddingLength);
|
content = malloc(content_len + header.paddingLength);
|
||||||
|
|
||||||
long rcv_len = 0;
|
long rcv_len = 0;
|
||||||
@ -529,10 +523,8 @@ int fastcgi_receive(fastcgi_cnx_t *conn, sock *client, unsigned long len) {
|
|||||||
FCGI_Header header = {
|
FCGI_Header header = {
|
||||||
.version = FCGI_VERSION_1,
|
.version = FCGI_VERSION_1,
|
||||||
.type = FCGI_STDIN,
|
.type = FCGI_STDIN,
|
||||||
.requestIdB1 = conn->req_id >> 8,
|
.requestId = htons(conn->req_id),
|
||||||
.requestIdB0 = conn->req_id & 0xFF,
|
.contentLength = htons(0),
|
||||||
.contentLengthB1 = 0,
|
|
||||||
.contentLengthB0 = 0,
|
|
||||||
.paddingLength = 0,
|
.paddingLength = 0,
|
||||||
.reserved = 0
|
.reserved = 0
|
||||||
};
|
};
|
||||||
@ -545,8 +537,7 @@ int fastcgi_receive(fastcgi_cnx_t *conn, sock *client, unsigned long len) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
rcv_len += ret;
|
rcv_len += ret;
|
||||||
header.contentLengthB1 = (ret >> 8) & 0xFF;
|
header.contentLength = htons(ret);
|
||||||
header.contentLengthB0 = ret & 0xFF;
|
|
||||||
if (send(conn->socket, &header, sizeof(header), 0) != sizeof(header)) goto err;
|
if (send(conn->socket, &header, sizeof(header), 0) != sizeof(header)) goto err;
|
||||||
if (send(conn->socket, buf, ret, 0) != ret) {
|
if (send(conn->socket, buf, ret, 0) != ret) {
|
||||||
err:
|
err:
|
||||||
|
@ -15,10 +15,8 @@
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
unsigned char version;
|
unsigned char version;
|
||||||
unsigned char type;
|
unsigned char type;
|
||||||
unsigned char requestIdB1;
|
unsigned short requestId;
|
||||||
unsigned char requestIdB0;
|
unsigned short contentLength;
|
||||||
unsigned char contentLengthB1;
|
|
||||||
unsigned char contentLengthB0;
|
|
||||||
unsigned char paddingLength;
|
unsigned char paddingLength;
|
||||||
unsigned char reserved;
|
unsigned char reserved;
|
||||||
} FCGI_Header;
|
} FCGI_Header;
|
||||||
|
Reference in New Issue
Block a user