forked from Mirrors/freeswitch
add libscgi (SCGI Client)
This commit is contained in:
parent
5a059ef630
commit
6bb33d7683
|
@ -0,0 +1,32 @@
|
|||
PWD=$(shell pwd)
|
||||
INCS=-I$(PWD)/src/include
|
||||
DEBUG=-g -ggdb
|
||||
BASE_FLAGS=$(INCS) $(DEBUG) -fPIC
|
||||
PICKY=-O2
|
||||
CFLAGS=$(BASE_FLAGS) $(PICKY)
|
||||
CXXFLAGS=$(BASE_FLAGS)
|
||||
MYLIB=libscgi.a
|
||||
LIBS=
|
||||
LDFLAGS=-L.
|
||||
OBJS=src/scgi.o
|
||||
SRC=src/scgi.c
|
||||
HEADERS=src/include/scgi.h
|
||||
SOLINK=-shared -Xlinker -x
|
||||
|
||||
|
||||
all: $(MYLIB) testclient
|
||||
|
||||
$(MYLIB): $(OBJS) $(HEADERS) $(SRC)
|
||||
ar rcs $(MYLIB) $(OBJS)
|
||||
ranlib $(MYLIB)
|
||||
|
||||
%.o: %.c $(HEADERS)
|
||||
$(CC) $(CC_CFLAGS) $(CFLAGS) -c $< -o $@
|
||||
|
||||
testclient: $(MYLIB) testclient.c
|
||||
$(CC) $(CC_CFLAGS) $(CFLAGS) testclient.c -o testclient -lscgi $(LDFLAGS) $(LIBS)
|
||||
|
||||
|
||||
|
||||
clean:
|
||||
rm -f *.o src/*.o libscgi.a *~ src/*~ src/include/*~ testclient
|
|
@ -0,0 +1,101 @@
|
|||
SCGI: A Simple Common Gateway Interface alternative
|
||||
Neil Schemenauer <nas@python.ca>
|
||||
2008-06-23
|
||||
|
||||
1. Introduction
|
||||
|
||||
The SCGI protocol is a replacement for the Common Gateway Interface
|
||||
(CGI) protocol. It is a standard for applications to interface with
|
||||
HTTP servers. It is similar to FastCGI but is designed to be easier
|
||||
to implement.
|
||||
|
||||
In this document, a string of 8-bit bytes may be written in two
|
||||
different forms: as a series of hexadecimal numbers between angle
|
||||
brackets, or as a sequence of ASCII characters between double quotes.
|
||||
For example, <68 65 6c 6c 6f 20 77 6f 72 6c 64 21> is a string of
|
||||
length 12; it is the same as the string "hello world!". Note that
|
||||
these notations are part of this document, not part of the protocol.
|
||||
|
||||
|
||||
2. Protocol
|
||||
|
||||
The client connects to a SCGI server over a reliable stream protocol
|
||||
allowing transmission of 8-bit bytes. The client begins by sending a
|
||||
request. See section 3 for the format of the request. When the SCGI
|
||||
server sees the end of the request it sends back a response and closes
|
||||
the connection. The format of the response is not specified by this
|
||||
protocol.
|
||||
|
||||
|
||||
3. Request Format
|
||||
|
||||
A request consists of a number of headers and a body. The format of
|
||||
the headers is:
|
||||
|
||||
headers ::= header*
|
||||
header ::= name NUL value NUL
|
||||
name ::= notnull+
|
||||
value ::= notnull*
|
||||
notnull ::= <01> | <02> | <03> | ... | <ff>
|
||||
NUL = <00>
|
||||
|
||||
Duplicate names are not allowed in the headers. The first header
|
||||
must have the name "CONTENT_LENGTH" and a value that is a nonempty
|
||||
sequence of ASCII digits giving the of the body length in decimal.
|
||||
The "CONTENT_LENGTH" header must always be present, even if its
|
||||
value is "0". There must also always be a header with the name
|
||||
"SCGI" and a value of "1". In order to facilitate the transition
|
||||
from CGI, standard CGI environment variables should be provided as
|
||||
SCGI headers.
|
||||
|
||||
The headers are sent encoded as a netstring. Netstring encoding is
|
||||
explained in section 4. The body is sent following the headers and
|
||||
its length is specified by the "CONTENT_LENGTH" header.
|
||||
|
||||
|
||||
4. Netstrings
|
||||
|
||||
Any string of 8-bit bytes may be encoded as [len]":"[string]",". Here
|
||||
[string] is the string and [len] is a nonempty sequence of ASCII
|
||||
digits giving the length of [string] in decimal. The ASCII digits are
|
||||
<30> for 0, <31> for 1, and so on up through <39> for 9. Extra zeros
|
||||
at the front of [len] are prohibited: [len] begins with <30> exactly
|
||||
when [string] is empty.
|
||||
|
||||
For example, the string "hello world!" is encoded as <31 32 3a 68 65
|
||||
6c 6c 6f 20 77 6f 72 6c 64 21 2c>, i.e., "12:hello world!,". The empty
|
||||
string is encoded as "0:,".
|
||||
|
||||
[len]":"[string]"," is called a netstring. [string] is called the
|
||||
interpretation of the netstring.
|
||||
|
||||
|
||||
5. Example
|
||||
|
||||
The web server (a SCGI client) opens a connection and sends the
|
||||
concatenation of the following strings:
|
||||
|
||||
"70:"
|
||||
"CONTENT_LENGTH" <00> "27" <00>
|
||||
"SCGI" <00> "1" <00>
|
||||
"REQUEST_METHOD" <00> "POST" <00>
|
||||
"REQUEST_URI" <00> "/deepthought" <00>
|
||||
","
|
||||
"What is the answer to life?"
|
||||
|
||||
The SCGI server sends the following response:
|
||||
|
||||
"Status: 200 OK" <0d 0a>
|
||||
"Content-Type: text/plain" <0d 0a>
|
||||
"" <0d 0a>
|
||||
"42"
|
||||
|
||||
The SCGI server closes the connection.
|
||||
|
||||
|
||||
6. Copyright
|
||||
|
||||
This document has been placed in the public domain.
|
||||
|
||||
|
||||
/* vim: set ai tw=74 et sw=4 sts=4: */
|
|
@ -0,0 +1,210 @@
|
|||
/*
|
||||
* Copyright (c) 2012-2013, Anthony Minessale II
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of the original author; nor the names of any contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
||||
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef _SCGI_H_
|
||||
#define _SCGI_H_
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* defined(__cplusplus) */
|
||||
#if EMACS_BUG
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define FD_SETSIZE 8192
|
||||
#define SCGI_USE_SELECT
|
||||
#else
|
||||
#define SCGI_USE_POLL
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifdef SCGI_USE_POLL
|
||||
#include <poll.h>
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
typedef SOCKET scgi_socket_t;
|
||||
typedef unsigned __int64 uint64_t;
|
||||
typedef unsigned __int32 uint32_t;
|
||||
typedef unsigned __int16 uint16_t;
|
||||
typedef unsigned __int8 uint8_t;
|
||||
typedef __int64 int64_t;
|
||||
typedef __int32 int32_t;
|
||||
typedef __int16 int16_t;
|
||||
typedef __int8 int8_t;
|
||||
typedef intptr_t scgi_ssize_t;
|
||||
typedef int scgi_filehandle_t;
|
||||
#define SCGI_SOCK_INVALID INVALID_SOCKET
|
||||
#define strerror_r(num, buf, size) strerror_s(buf, size, num)
|
||||
#if defined(SCGI_DECLARE_STATIC)
|
||||
#define SCGI_DECLARE(type) type __stdcall
|
||||
#define SCGI_DECLARE_NONSTD(type) type __cdecl
|
||||
#define SCGI_DECLARE_DATA
|
||||
#elif defined(SCGI_EXPORTS)
|
||||
#define SCGI_DECLARE(type) __declspec(dllexport) type __stdcall
|
||||
#define SCGI_DECLARE_NONSTD(type) __declspec(dllexport) type __cdecl
|
||||
#define SCGI_DECLARE_DATA __declspec(dllexport)
|
||||
#else
|
||||
#define SCGI_DECLARE(type) __declspec(dllimport) type __stdcall
|
||||
#define SCGI_DECLARE_NONSTD(type) __declspec(dllimport) type __cdecl
|
||||
#define SCGI_DECLARE_DATA __declspec(dllimport)
|
||||
#endif
|
||||
#else
|
||||
#define SCGI_DECLARE(type) type
|
||||
#define SCGI_DECLARE_NONSTD(type) type
|
||||
#define SCGI_DECLARE_DATA
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <stdarg.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#define SCGI_SOCK_INVALID -1
|
||||
typedef int scgi_socket_t;
|
||||
typedef ssize_t scgi_ssize_t;
|
||||
typedef int scgi_filehandle_t;
|
||||
#endif
|
||||
|
||||
|
||||
#include <time.h>
|
||||
#ifndef WIN32
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#ifndef WIN32
|
||||
#include <sys/types.h>
|
||||
#include <sys/select.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <sys/signal.h>
|
||||
#include <unistd.h>
|
||||
#include <ctype.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_STRINGS_H
|
||||
#include <strings.h>
|
||||
#endif
|
||||
#include <assert.h>
|
||||
|
||||
#if (_MSC_VER >= 1400) // VC8+
|
||||
#define scgi_assert(expr) assert(expr);__analysis_assume( expr )
|
||||
#endif
|
||||
|
||||
#ifndef scgi_assert
|
||||
#define scgi_assert(_x) assert(_x)
|
||||
#endif
|
||||
|
||||
#define scgi_safe_free(_x) if (_x) free(_x); _x = NULL
|
||||
#define scgi_strlen_zero(s) (!s || *(s) == '\0')
|
||||
#define scgi_strlen_zero_buf(s) (*(s) == '\0')
|
||||
#define end_of(_s) *(*_s == '\0' ? _s : _s + strlen(_s) - 1)
|
||||
|
||||
|
||||
typedef enum {
|
||||
SCGI_POLL_READ = (1 << 0),
|
||||
SCGI_POLL_WRITE = (1 << 1),
|
||||
SCGI_POLL_ERROR = (1 << 2)
|
||||
} scgi_poll_t;
|
||||
|
||||
|
||||
typedef struct scgi_param_s {
|
||||
char *name;
|
||||
char *value;
|
||||
struct scgi_param_s *next;
|
||||
} scgi_param_t;
|
||||
|
||||
typedef struct scgi_handle_s {
|
||||
scgi_param_t *params;
|
||||
char *body;
|
||||
struct sockaddr_in sockaddr;
|
||||
struct hostent hostent;
|
||||
char hostbuf[256];
|
||||
scgi_socket_t sock;
|
||||
char err[256];
|
||||
int errnum;
|
||||
int connected;
|
||||
struct sockaddr_in addr;
|
||||
int destroyed;
|
||||
} scgi_handle_t;
|
||||
|
||||
|
||||
typedef int16_t scgi_port_t;
|
||||
typedef size_t scgi_size_t;
|
||||
|
||||
typedef enum {
|
||||
SCGI_SUCCESS,
|
||||
SCGI_FAIL,
|
||||
SCGI_BREAK,
|
||||
SCGI_DISCONNECTED,
|
||||
SCGI_GENERR
|
||||
} scgi_status_t;
|
||||
|
||||
|
||||
|
||||
SCGI_DECLARE(scgi_status_t) scgi_connect(scgi_handle_t *handle, const char *host, scgi_port_t port, uint32_t timeout);
|
||||
SCGI_DECLARE(scgi_status_t) scgi_disconnect(scgi_handle_t *handle);
|
||||
SCGI_DECLARE(int) scgi_wait_sock(scgi_socket_t sock, uint32_t ms, scgi_poll_t flags);
|
||||
SCGI_DECLARE(ssize_t) scgi_recv(scgi_handle_t *handle, unsigned char *buf, size_t buflen);
|
||||
SCGI_DECLARE(scgi_status_t) scgi_send_request(scgi_handle_t *handle);
|
||||
SCGI_DECLARE(scgi_status_t) scgi_add_param(scgi_handle_t *handle, const char *name, const char *value);
|
||||
SCGI_DECLARE(scgi_status_t) scgi_add_body(scgi_handle_t *handle, const char *value);
|
||||
SCGI_DECLARE(size_t) scgi_build_message(scgi_handle_t *handle, char **buffer);
|
||||
SCGI_DECLARE(scgi_status_t) scgi_destroy_params(scgi_handle_t *handle);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* defined(__cplusplus) */
|
||||
|
||||
|
||||
#endif /* defined(_SCGI_H_) */
|
||||
|
||||
/* For Emacs:
|
||||
* Local Variables:
|
||||
* mode:c
|
||||
* indent-tabs-mode:t
|
||||
* tab-width:4
|
||||
* c-basic-offset:4
|
||||
* End:
|
||||
* For VIM:
|
||||
* vim:set softtabstop=4 shiftwidth=4 tabstop=4:
|
||||
*/
|
|
@ -0,0 +1,520 @@
|
|||
/*
|
||||
* Copyright (c) 2012-2013, Anthony Minessale II
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of the original author; nor the names of any contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
||||
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <scgi.h>
|
||||
|
||||
#ifndef WIN32
|
||||
#define closesocket(x) shutdown(x, 2); close(x)
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#else
|
||||
#pragma warning (disable:6386)
|
||||
/* These warnings need to be ignored warning in sdk header */
|
||||
#include <Ws2tcpip.h>
|
||||
#include <windows.h>
|
||||
#ifndef errno
|
||||
#define errno WSAGetLastError()
|
||||
#endif
|
||||
#ifndef EINTR
|
||||
#define EINTR WSAEINTR
|
||||
#endif
|
||||
#pragma warning (default:6386)
|
||||
#endif
|
||||
|
||||
static scgi_status_t scgi_push_param(scgi_handle_t *handle, const char *name, const char *value);
|
||||
|
||||
static int sock_setup(scgi_handle_t *handle)
|
||||
{
|
||||
|
||||
if (handle->sock == SCGI_SOCK_INVALID) {
|
||||
return SCGI_FAIL;
|
||||
}
|
||||
|
||||
#ifdef WIN32
|
||||
{
|
||||
BOOL bOptVal = TRUE;
|
||||
int bOptLen = sizeof(BOOL);
|
||||
setsockopt(handle->sock, IPPROTO_TCP, TCP_NODELAY, (const char *)&bOptVal, bOptLen);
|
||||
}
|
||||
#else
|
||||
{
|
||||
int x = 1;
|
||||
setsockopt(handle->sock, IPPROTO_TCP, TCP_NODELAY, &x, sizeof(x));
|
||||
}
|
||||
#endif
|
||||
|
||||
return SCGI_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
SCGI_DECLARE(size_t) scgi_build_message(scgi_handle_t *handle, char **bufferp)
|
||||
{
|
||||
scgi_param_t *pp;
|
||||
size_t len = 0, plen = 0, ctlen = 0;
|
||||
char *s, *bp;
|
||||
char *buffer = NULL;
|
||||
char tmp[128] = "";
|
||||
|
||||
scgi_push_param(handle, "SCGI", "1");
|
||||
|
||||
if (handle->body) {
|
||||
ctlen = strlen(handle->body);
|
||||
}
|
||||
|
||||
snprintf(tmp, sizeof(tmp), "%ld", ctlen);
|
||||
|
||||
scgi_push_param(handle, "CONTENT_LENGTH", tmp);
|
||||
|
||||
|
||||
for(pp = handle->params; pp; pp = pp->next) {
|
||||
plen += (strlen(pp->name) + strlen(pp->value) + 2);
|
||||
}
|
||||
|
||||
snprintf(tmp, sizeof(tmp), "%ld", plen + ctlen);
|
||||
|
||||
len = plen + ctlen + strlen(tmp) + 2;
|
||||
|
||||
buffer = malloc(len);
|
||||
memset(buffer, 0, len);
|
||||
|
||||
snprintf(buffer, len, "%ld:", plen);
|
||||
bp = buffer + strlen(buffer);
|
||||
|
||||
for(pp = handle->params; pp; pp = pp->next) {
|
||||
|
||||
for (s = pp->name; s && *s; s++) {
|
||||
*bp++ = *s;
|
||||
}
|
||||
|
||||
*bp++ = '\0';
|
||||
|
||||
for (s = pp->value; s && *s; s++) {
|
||||
*bp++ = *s;
|
||||
}
|
||||
|
||||
*bp++ = '\0';
|
||||
}
|
||||
|
||||
*bp++ = ',';
|
||||
|
||||
if (handle->body) {
|
||||
for (s = handle->body; s && *s; s++) {
|
||||
*bp++ = *s;
|
||||
}
|
||||
}
|
||||
|
||||
*bufferp = buffer;
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
SCGI_DECLARE(scgi_status_t) scgi_destroy_params(scgi_handle_t *handle)
|
||||
{
|
||||
scgi_param_t *param, *pp;
|
||||
|
||||
pp = handle->params;
|
||||
|
||||
while(pp) {
|
||||
param = pp;
|
||||
pp = pp->next;
|
||||
|
||||
free(param->name);
|
||||
free(param->value);
|
||||
free(param);
|
||||
}
|
||||
|
||||
handle->params = NULL;
|
||||
|
||||
return SCGI_SUCCESS;
|
||||
}
|
||||
|
||||
SCGI_DECLARE(scgi_status_t) scgi_add_body(scgi_handle_t *handle, const char *value)
|
||||
{
|
||||
handle->body = strdup(value);
|
||||
|
||||
return SCGI_SUCCESS;
|
||||
}
|
||||
|
||||
SCGI_DECLARE(scgi_status_t) scgi_add_param(scgi_handle_t *handle, const char *name, const char *value)
|
||||
{
|
||||
scgi_param_t *param, *pp;
|
||||
|
||||
for(pp = handle->params; pp && pp->next; pp = pp->next) {
|
||||
if (!strcasecmp(pp->name, name)) {
|
||||
return SCGI_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
param = malloc(sizeof(*param));
|
||||
memset(param, 0, sizeof(*param));
|
||||
|
||||
param->name = strdup(name);
|
||||
param->value = strdup(value);
|
||||
|
||||
if (!pp) {
|
||||
handle->params = param;
|
||||
} else {
|
||||
pp->next = param;
|
||||
}
|
||||
|
||||
return SCGI_SUCCESS;
|
||||
}
|
||||
|
||||
static scgi_status_t scgi_push_param(scgi_handle_t *handle, const char *name, const char *value)
|
||||
{
|
||||
scgi_param_t *param;
|
||||
|
||||
param = malloc(sizeof(*param));
|
||||
memset(param, 0, sizeof(*param));
|
||||
|
||||
param->name = strdup(name);
|
||||
param->value = strdup(value);
|
||||
|
||||
param->next = handle->params;
|
||||
handle->params = param;
|
||||
|
||||
return SCGI_SUCCESS;
|
||||
}
|
||||
|
||||
SCGI_DECLARE(scgi_status_t) scgi_send_request(scgi_handle_t *handle)
|
||||
{
|
||||
scgi_status_t status;
|
||||
char *buffer = NULL;
|
||||
size_t bytes = 0;
|
||||
ssize_t sent = 0;
|
||||
|
||||
if (handle->connected != 1) {
|
||||
return SCGI_FAIL;
|
||||
}
|
||||
|
||||
bytes = scgi_build_message(handle, &buffer);
|
||||
sent = send(handle->sock, buffer, bytes, 0);
|
||||
|
||||
if (sent <= 0) {
|
||||
handle->connected = -1;
|
||||
}
|
||||
|
||||
scgi_safe_free(buffer);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
SCGI_DECLARE(ssize_t) scgi_recv(scgi_handle_t *handle, unsigned char *buf, size_t buflen)
|
||||
{
|
||||
ssize_t recvd;
|
||||
|
||||
if (handle->connected != 1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
recvd = recv(handle->sock, buf, buflen, 0);
|
||||
|
||||
if (recvd == 0) {
|
||||
handle->connected = -1;
|
||||
}
|
||||
|
||||
|
||||
return recvd;
|
||||
}
|
||||
|
||||
SCGI_DECLARE(scgi_status_t) scgi_connect(scgi_handle_t *handle, const char *host, scgi_port_t port, uint32_t timeout)
|
||||
{
|
||||
int rval = 0;
|
||||
|
||||
struct addrinfo hints = { 0 }, *result;
|
||||
#ifndef WIN32
|
||||
int fd_flags = 0;
|
||||
#else
|
||||
WORD wVersionRequested = MAKEWORD(2, 0);
|
||||
WSADATA wsaData;
|
||||
int err = WSAStartup(wVersionRequested, &wsaData);
|
||||
if (err != 0) {
|
||||
snprintf(handle->err, sizeof(handle->err), "WSAStartup Error");
|
||||
return SCGI_FAIL;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
handle->sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
|
||||
if (handle->sock == SCGI_SOCK_INVALID) {
|
||||
snprintf(handle->err, sizeof(handle->err), "Socket Error");
|
||||
return SCGI_FAIL;
|
||||
}
|
||||
|
||||
|
||||
hints.ai_family = AF_INET;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
|
||||
if (getaddrinfo(host, NULL, &hints, &result)) {
|
||||
strncpy(handle->err, "Cannot resolve host", sizeof(handle->err));
|
||||
goto fail;
|
||||
}
|
||||
|
||||
memcpy(&handle->sockaddr, result->ai_addr, sizeof(handle->sockaddr));
|
||||
handle->sockaddr.sin_family = AF_INET;
|
||||
handle->sockaddr.sin_port = htons(port);
|
||||
freeaddrinfo(result);
|
||||
|
||||
if (timeout) {
|
||||
#ifdef WIN32
|
||||
u_long arg = 1;
|
||||
if (ioctlsocket(handle->sock, FIONBIO, &arg) == SOCKET_ERROR) {
|
||||
snprintf(handle->err, sizeof(handle->err), "Socket Connection Error");
|
||||
goto fail;
|
||||
}
|
||||
#else
|
||||
fd_flags = fcntl(handle->sock, F_GETFL, 0);
|
||||
if (fcntl(handle->sock, F_SETFL, fd_flags | O_NONBLOCK)) {
|
||||
snprintf(handle->err, sizeof(handle->err), "Socket Connection Error");
|
||||
goto fail;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
rval = connect(handle->sock, (struct sockaddr*)&handle->sockaddr, sizeof(handle->sockaddr));
|
||||
|
||||
if (timeout) {
|
||||
int r;
|
||||
|
||||
|
||||
r = scgi_wait_sock(handle->sock, timeout, SCGI_POLL_WRITE);
|
||||
|
||||
if (r <= 0) {
|
||||
snprintf(handle->err, sizeof(handle->err), "Connection timed out");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (!(r & SCGI_POLL_WRITE)) {
|
||||
snprintf(handle->err, sizeof(handle->err), "Connection timed out");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
#ifdef WIN32
|
||||
{
|
||||
u_long arg = 0;
|
||||
if (ioctlsocket(handle->sock, FIONBIO, &arg) == SOCKET_ERROR) {
|
||||
snprintf(handle->err, sizeof(handle->err), "Socket Connection Error");
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
#else
|
||||
fcntl(handle->sock, F_SETFL, fd_flags);
|
||||
#endif
|
||||
rval = 0;
|
||||
}
|
||||
|
||||
result = NULL;
|
||||
|
||||
if (rval) {
|
||||
snprintf(handle->err, sizeof(handle->err), "Socket Connection Error");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
sock_setup(handle);
|
||||
|
||||
handle->connected = 1;
|
||||
|
||||
|
||||
return SCGI_SUCCESS;
|
||||
|
||||
fail:
|
||||
|
||||
handle->connected = 0;
|
||||
scgi_disconnect(handle);
|
||||
|
||||
return SCGI_FAIL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
SCGI_DECLARE(scgi_status_t) scgi_disconnect(scgi_handle_t *handle)
|
||||
{
|
||||
scgi_status_t status = SCGI_FAIL;
|
||||
|
||||
if (handle->destroyed) {
|
||||
return SCGI_FAIL;
|
||||
}
|
||||
|
||||
handle->destroyed = 1;
|
||||
handle->connected = 0;
|
||||
|
||||
scgi_destroy_params(handle);
|
||||
scgi_safe_free(handle->body);
|
||||
|
||||
if (handle->sock != SCGI_SOCK_INVALID) {
|
||||
closesocket(handle->sock);
|
||||
handle->sock = SCGI_SOCK_INVALID;
|
||||
status = SCGI_SUCCESS;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/* USE WSAPoll on vista or higher */
|
||||
#ifdef SCGI_USE_WSAPOLL
|
||||
SCGI_DECLARE(int) scgi_wait_sock(scgi_socket_t sock, uint32_t ms, scgi_poll_t flags)
|
||||
{
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef SCGI_USE_SELECT
|
||||
#ifdef WIN32
|
||||
#pragma warning( push )
|
||||
#pragma warning( disable : 6262 ) /* warning C6262: Function uses '98348' bytes of stack: exceeds /analyze:stacksize'16384'. Consider moving some data to heap */
|
||||
#endif
|
||||
SCGI_DECLARE(int) scgi_wait_sock(scgi_socket_t sock, uint32_t ms, scgi_poll_t flags)
|
||||
{
|
||||
int s = 0, r = 0;
|
||||
fd_set rfds;
|
||||
fd_set wfds;
|
||||
fd_set efds;
|
||||
struct timeval tv;
|
||||
|
||||
FD_ZERO(&rfds);
|
||||
FD_ZERO(&wfds);
|
||||
FD_ZERO(&efds);
|
||||
|
||||
#ifndef WIN32
|
||||
/* Wouldn't you rather know?? */
|
||||
assert(sock <= FD_SETSIZE);
|
||||
#endif
|
||||
|
||||
if ((flags & SCGI_POLL_READ)) {
|
||||
|
||||
#ifdef WIN32
|
||||
#pragma warning( push )
|
||||
#pragma warning( disable : 4127 )
|
||||
FD_SET(sock, &rfds);
|
||||
#pragma warning( pop )
|
||||
#else
|
||||
FD_SET(sock, &rfds);
|
||||
#endif
|
||||
}
|
||||
|
||||
if ((flags & SCGI_POLL_WRITE)) {
|
||||
|
||||
#ifdef WIN32
|
||||
#pragma warning( push )
|
||||
#pragma warning( disable : 4127 )
|
||||
FD_SET(sock, &wfds);
|
||||
#pragma warning( pop )
|
||||
#else
|
||||
FD_SET(sock, &wfds);
|
||||
#endif
|
||||
}
|
||||
|
||||
if ((flags & SCGI_POLL_ERROR)) {
|
||||
|
||||
#ifdef WIN32
|
||||
#pragma warning( push )
|
||||
#pragma warning( disable : 4127 )
|
||||
FD_SET(sock, &efds);
|
||||
#pragma warning( pop )
|
||||
#else
|
||||
FD_SET(sock, &efds);
|
||||
#endif
|
||||
}
|
||||
|
||||
tv.tv_sec = ms / 1000;
|
||||
tv.tv_usec = (ms % 1000) * ms;
|
||||
|
||||
s = select(sock + 1, (flags & SCGI_POLL_READ) ? &rfds : NULL, (flags & SCGI_POLL_WRITE) ? &wfds : NULL, (flags & SCGI_POLL_ERROR) ? &efds : NULL, &tv);
|
||||
|
||||
if (s < 0) {
|
||||
r = s;
|
||||
} else if (s > 0) {
|
||||
if ((flags & SCGI_POLL_READ) && FD_ISSET(sock, &rfds)) {
|
||||
r |= SCGI_POLL_READ;
|
||||
}
|
||||
|
||||
if ((flags & SCGI_POLL_WRITE) && FD_ISSET(sock, &wfds)) {
|
||||
r |= SCGI_POLL_WRITE;
|
||||
}
|
||||
|
||||
if ((flags & SCGI_POLL_ERROR) && FD_ISSET(sock, &efds)) {
|
||||
r |= SCGI_POLL_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
return r;
|
||||
|
||||
}
|
||||
#ifdef WIN32
|
||||
#pragma warning( pop )
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef SCGI_USE_POLL
|
||||
SCGI_DECLARE(int) scgi_wait_sock(scgi_socket_t sock, uint32_t ms, scgi_poll_t flags)
|
||||
{
|
||||
struct pollfd pfds[2] = { { 0 } };
|
||||
int s = 0, r = 0;
|
||||
|
||||
pfds[0].fd = sock;
|
||||
|
||||
if ((flags & SCGI_POLL_READ)) {
|
||||
pfds[0].events |= POLLIN;
|
||||
}
|
||||
|
||||
if ((flags & SCGI_POLL_WRITE)) {
|
||||
pfds[0].events |= POLLOUT;
|
||||
}
|
||||
|
||||
if ((flags & SCGI_POLL_ERROR)) {
|
||||
pfds[0].events |= POLLERR;
|
||||
}
|
||||
|
||||
s = poll(pfds, 1, ms);
|
||||
|
||||
if (s < 0) {
|
||||
r = s;
|
||||
} else if (s > 0) {
|
||||
if ((pfds[0].revents & POLLIN)) {
|
||||
r |= SCGI_POLL_READ;
|
||||
}
|
||||
if ((pfds[0].revents & POLLOUT)) {
|
||||
r |= SCGI_POLL_WRITE;
|
||||
}
|
||||
if ((pfds[0].revents & POLLERR)) {
|
||||
r |= SCGI_POLL_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
return r;
|
||||
|
||||
}
|
||||
#endif
|
|
@ -0,0 +1,39 @@
|
|||
#include <scgi.h>
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char buf[16336] = "";
|
||||
ssize_t len;
|
||||
scgi_handle_t handle = { 0 };
|
||||
char *ip;
|
||||
int port = 0;
|
||||
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "usage: testclient <ip> <port>");
|
||||
}
|
||||
|
||||
ip = argv[1];
|
||||
port = atoi(argv[2]);
|
||||
|
||||
|
||||
|
||||
scgi_add_param(&handle, "REQUEST_METHOD", "POST");
|
||||
scgi_add_param(&handle, "REQUEST_URI", "/deepthought");
|
||||
scgi_add_param(&handle, "TESTING", "TRUE");
|
||||
scgi_add_param(&handle, "TESTING", "TRUE");
|
||||
scgi_add_body(&handle, "What is the answer to life?");
|
||||
|
||||
|
||||
scgi_connect(&handle, ip, port, 10000);
|
||||
|
||||
scgi_send_request(&handle);
|
||||
|
||||
|
||||
while((len = scgi_recv(&handle, buf, sizeof(buf))) > 0) {
|
||||
printf("READ [%s]\n", buf);
|
||||
}
|
||||
|
||||
scgi_disconnect(&handle);
|
||||
|
||||
}
|
Loading…
Reference in New Issue