2011-04-27 17:18:17 +00:00
|
|
|
/**
|
|
|
|
* libpsyc test server for packet parsing & rendering
|
2011-04-28 13:20:07 +00:00
|
|
|
*
|
2011-04-27 17:18:17 +00:00
|
|
|
* based on selectserver.c from http://beej.us/guide/bgnet/
|
2011-04-28 13:20:07 +00:00
|
|
|
* "The C source code presented in this document is hereby granted to the public domain, and is completely free of any license restriction."
|
2011-04-27 17:18:17 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2011-05-05 22:15:37 +00:00
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/time.h>
|
2011-04-27 17:18:17 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2011-05-05 22:15:37 +00:00
|
|
|
#define __USE_POSIX
|
2011-04-27 17:18:17 +00:00
|
|
|
#include <netdb.h>
|
2011-04-29 01:20:00 +00:00
|
|
|
#include <math.h>
|
2011-04-30 13:46:17 +00:00
|
|
|
#include <assert.h>
|
2011-04-27 17:18:17 +00:00
|
|
|
|
2011-04-30 13:46:17 +00:00
|
|
|
#include <psyc.h>
|
2011-04-27 17:18:17 +00:00
|
|
|
#include <psyc/parser.h>
|
|
|
|
#include <psyc/render.h>
|
2011-04-29 01:20:00 +00:00
|
|
|
#include <psyc/syntax.h>
|
2011-04-27 17:18:17 +00:00
|
|
|
|
2011-05-05 22:15:37 +00:00
|
|
|
const size_t RECV_BUF_SIZE = 8 * 1024;
|
|
|
|
const size_t CONT_BUF_SIZE = 8 * 1024;
|
|
|
|
const size_t SEND_BUF_SIZE = 8 * 1024;
|
2011-04-27 17:18:17 +00:00
|
|
|
const size_t NUM_PARSERS = 100;
|
2011-04-29 01:20:00 +00:00
|
|
|
// max size for routing & entity header
|
|
|
|
const size_t ROUTING_LINES = 16;
|
|
|
|
const size_t ENTITY_LINES = 32;
|
2011-04-27 17:18:17 +00:00
|
|
|
|
2011-05-05 22:15:37 +00:00
|
|
|
static inline
|
|
|
|
void resetString (psycString *s, uint8_t freeptr)
|
|
|
|
{
|
|
|
|
if (freeptr && s->length)
|
|
|
|
free((void*)s->ptr);
|
|
|
|
|
|
|
|
s->ptr = NULL;
|
|
|
|
s->length = 0;
|
|
|
|
}
|
|
|
|
|
2011-04-27 17:18:17 +00:00
|
|
|
// get sockaddr, IPv4 or IPv6:
|
2011-05-03 23:00:35 +00:00
|
|
|
void *get_in_addr (struct sockaddr *sa)
|
2011-04-27 17:18:17 +00:00
|
|
|
{
|
2011-05-03 23:00:35 +00:00
|
|
|
if (sa->sa_family == AF_INET)
|
2011-04-27 17:18:17 +00:00
|
|
|
return &(((struct sockaddr_in*)sa)->sin_addr);
|
|
|
|
|
|
|
|
return &(((struct sockaddr_in6*)sa)->sin6_addr);
|
|
|
|
}
|
|
|
|
|
2011-05-03 23:00:35 +00:00
|
|
|
int main (int argc, char **argv)
|
2011-04-27 17:18:17 +00:00
|
|
|
{
|
|
|
|
char *port = argc > 1 ? argv[1] : "4440";
|
2011-05-06 11:32:12 +00:00
|
|
|
char *opts = argc > 2 ? argv[2] : NULL;
|
|
|
|
char *v, *w;
|
|
|
|
uint8_t verbose = opts && (v = memchr(opts, (int)'v', strlen(opts))) ?
|
|
|
|
v - opts > 0 && (w = memchr(v+1, (int)'v', strlen(opts) - (v - opts))) ?
|
|
|
|
w - v > 0 && memchr(w+1, (int)'v', strlen(opts) - (w - opts)) ? 3 : 2 : 1 : 0;
|
|
|
|
uint8_t routing_only = opts && memchr(opts, (int)'r', strlen(opts));
|
|
|
|
uint8_t parse_multiple = opts && memchr(opts, (int)'m', strlen(opts));
|
|
|
|
uint8_t progress = opts && memchr(opts, (int)'p', strlen(opts));
|
|
|
|
uint8_t stats = opts && memchr(opts, (int)'s', strlen(opts));
|
2011-05-05 22:15:37 +00:00
|
|
|
size_t recv_buf_size = argc > 3 ? atoi(argv[3]) : 0;
|
|
|
|
if (recv_buf_size <= 0)
|
|
|
|
recv_buf_size = RECV_BUF_SIZE;
|
2011-04-27 17:18:17 +00:00
|
|
|
|
|
|
|
fd_set master; // master file descriptor list
|
|
|
|
fd_set read_fds; // temp file descriptor list for select()
|
|
|
|
int fdmax; // maximum file descriptor number
|
|
|
|
|
|
|
|
int listener; // listening socket descriptor
|
|
|
|
int newfd; // newly accept()ed socket descriptor
|
|
|
|
struct sockaddr_storage remoteaddr; // client address
|
|
|
|
socklen_t addrlen;
|
|
|
|
|
2011-04-29 01:20:00 +00:00
|
|
|
char buf[CONT_BUF_SIZE + RECV_BUF_SIZE]; // cont buf + recv buf: [ ccrrrr]
|
|
|
|
char *recvbuf = buf + CONT_BUF_SIZE; // recv buf: ^^^^
|
|
|
|
char *parsebuf; // parse buf: ^^^^^^
|
2011-04-27 17:18:17 +00:00
|
|
|
char sendbuf[SEND_BUF_SIZE];
|
|
|
|
size_t nbytes, contbytes = 0;
|
|
|
|
|
|
|
|
char remoteIP[INET6_ADDRSTRLEN];
|
|
|
|
|
|
|
|
int yes = 1; // for setsockopt() SO_REUSEADDR, below
|
2011-05-05 22:15:37 +00:00
|
|
|
int i, j, rv;
|
2011-04-27 17:18:17 +00:00
|
|
|
|
|
|
|
struct addrinfo hints, *ai, *p;
|
|
|
|
|
|
|
|
psycParseState parsers[NUM_PARSERS];
|
|
|
|
psycPacket packets[NUM_PARSERS];
|
2011-04-29 01:20:00 +00:00
|
|
|
psycModifier routing[NUM_PARSERS][ROUTING_LINES];
|
|
|
|
psycModifier entity[NUM_PARSERS][ENTITY_LINES];
|
2011-05-03 23:00:35 +00:00
|
|
|
psycModifier *mod = NULL;
|
2011-04-27 17:18:17 +00:00
|
|
|
|
2011-05-05 22:15:37 +00:00
|
|
|
struct timeval start[NUM_PARSERS], end[NUM_PARSERS];
|
|
|
|
|
2011-04-27 17:18:17 +00:00
|
|
|
int ret, retl;
|
|
|
|
char oper;
|
|
|
|
psycString name, value, elem;
|
2011-05-03 23:00:35 +00:00
|
|
|
psycString *pname = NULL, *pvalue = NULL;
|
2011-04-27 17:18:17 +00:00
|
|
|
psycParseListState listState;
|
|
|
|
|
|
|
|
FD_ZERO(&master); // clear the master and temp sets
|
|
|
|
FD_ZERO(&read_fds);
|
|
|
|
|
|
|
|
// get us a socket and bind it
|
|
|
|
memset(&hints, 0, sizeof hints);
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_flags = AI_PASSIVE;
|
2011-05-03 23:00:35 +00:00
|
|
|
if ((rv = getaddrinfo(NULL, port, &hints, &ai)) != 0)
|
|
|
|
{
|
2011-04-27 17:18:17 +00:00
|
|
|
fprintf(stderr, "error: %s\n", gai_strerror(rv));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2011-05-03 23:00:35 +00:00
|
|
|
for (p = ai; p != NULL; p = p->ai_next)
|
|
|
|
{
|
2011-04-27 17:18:17 +00:00
|
|
|
listener = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
|
2011-05-03 23:00:35 +00:00
|
|
|
if (listener < 0)
|
2011-04-27 17:18:17 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// lose the pesky "address already in use" error message
|
|
|
|
setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
|
|
|
|
|
2011-05-03 23:00:35 +00:00
|
|
|
if (bind(listener, p->ai_addr, p->ai_addrlen) < 0)
|
|
|
|
{
|
2011-04-27 17:18:17 +00:00
|
|
|
close(listener);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we got here, it means we didn't get bound
|
2011-05-03 23:00:35 +00:00
|
|
|
if (p == NULL)
|
|
|
|
{
|
2011-04-27 17:18:17 +00:00
|
|
|
fprintf(stderr, "failed to bind\n");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo(ai); // all done with this
|
|
|
|
|
|
|
|
// listen
|
2011-05-03 23:00:35 +00:00
|
|
|
if (listen(listener, 10) == -1)
|
|
|
|
{
|
2011-04-27 17:18:17 +00:00
|
|
|
perror("listen");
|
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the listener to the master set
|
|
|
|
FD_SET(listener, &master);
|
|
|
|
|
|
|
|
// keep track of the biggest file descriptor
|
|
|
|
fdmax = listener; // so far, it's this one
|
|
|
|
|
|
|
|
// main loop
|
2011-05-03 23:00:35 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
2011-04-27 17:18:17 +00:00
|
|
|
read_fds = master; // copy it
|
2011-05-03 23:00:35 +00:00
|
|
|
if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1)
|
|
|
|
{
|
2011-04-27 17:18:17 +00:00
|
|
|
perror("select");
|
|
|
|
exit(4);
|
|
|
|
}
|
|
|
|
|
|
|
|
// run through the existing connections looking for data to read
|
2011-05-03 23:00:35 +00:00
|
|
|
for (i = 0; i <= fdmax; i++)
|
|
|
|
{
|
|
|
|
if (FD_ISSET(i, &read_fds)) // we got one!!
|
|
|
|
{
|
|
|
|
if (i == listener)
|
|
|
|
{
|
2011-04-27 17:18:17 +00:00
|
|
|
// handle new connections
|
|
|
|
if (fdmax == NUM_PARSERS - 1)
|
|
|
|
continue; // ignore if there's too many
|
|
|
|
|
|
|
|
addrlen = sizeof remoteaddr;
|
|
|
|
newfd = accept(listener, (struct sockaddr *)&remoteaddr, &addrlen);
|
|
|
|
|
2011-05-03 23:00:35 +00:00
|
|
|
if (newfd == -1)
|
2011-04-27 17:18:17 +00:00
|
|
|
perror("accept");
|
2011-05-03 23:00:35 +00:00
|
|
|
else
|
|
|
|
{
|
2011-04-27 17:18:17 +00:00
|
|
|
FD_SET(newfd, &master); // add to master set
|
2011-05-03 23:00:35 +00:00
|
|
|
if (newfd > fdmax) // keep track of the max
|
2011-04-27 17:18:17 +00:00
|
|
|
fdmax = newfd;
|
|
|
|
|
|
|
|
// reset parser state & packet
|
2011-04-30 15:07:01 +00:00
|
|
|
if (routing_only)
|
|
|
|
psyc_initParseState2(&parsers[newfd], PSYC_PARSE_ROUTING_ONLY);
|
|
|
|
else
|
|
|
|
psyc_initParseState(&parsers[newfd]);
|
2011-05-03 20:24:50 +00:00
|
|
|
|
2011-04-27 17:18:17 +00:00
|
|
|
memset(&packets[newfd], 0, sizeof(psycPacket));
|
2011-04-29 01:20:00 +00:00
|
|
|
memset(&routing[newfd], 0, sizeof(psycModifier) * ROUTING_LINES);
|
|
|
|
memset(&entity[newfd], 0, sizeof(psycModifier) * ENTITY_LINES);
|
|
|
|
packets[newfd].routing.modifiers = routing[newfd];
|
|
|
|
packets[newfd].entity.modifiers = entity[newfd];
|
2011-04-27 17:18:17 +00:00
|
|
|
|
2011-05-06 11:32:12 +00:00
|
|
|
if (verbose)
|
|
|
|
printf("# New connection from %s on socket %d\n",
|
|
|
|
inet_ntop(remoteaddr.ss_family,
|
|
|
|
get_in_addr((struct sockaddr*)&remoteaddr),
|
|
|
|
remoteIP, INET6_ADDRSTRLEN),
|
|
|
|
newfd);
|
2011-05-05 22:15:37 +00:00
|
|
|
|
|
|
|
if (stats)
|
|
|
|
gettimeofday(&start[newfd], NULL);
|
2011-04-27 17:18:17 +00:00
|
|
|
}
|
2011-05-03 23:00:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-27 17:18:17 +00:00
|
|
|
// handle data from a client
|
2011-05-05 22:15:37 +00:00
|
|
|
if ((nbytes = recv(i, recvbuf, recv_buf_size, 0)) <= 0)
|
2011-05-03 23:00:35 +00:00
|
|
|
{
|
2011-05-05 22:15:37 +00:00
|
|
|
if (stats)
|
|
|
|
printf("%ld ms\n", (end[i].tv_sec * 1000000 + end[i].tv_usec - start[i].tv_sec * 1000000 - start[i].tv_usec) / 1000);
|
|
|
|
|
2011-04-27 17:18:17 +00:00
|
|
|
// got error or connection closed by client
|
2011-05-03 23:00:35 +00:00
|
|
|
if (nbytes == 0) // connection closed
|
2011-05-06 11:32:12 +00:00
|
|
|
printf("# Socket %d hung up\n", i);
|
2011-05-03 23:00:35 +00:00
|
|
|
else
|
2011-04-27 17:18:17 +00:00
|
|
|
perror("recv");
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-04-27 17:18:17 +00:00
|
|
|
close(i); // bye!
|
|
|
|
FD_CLR(i, &master); // remove from master set
|
2011-05-03 23:00:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-27 17:18:17 +00:00
|
|
|
// we got some data from a client
|
|
|
|
parsebuf = recvbuf - contbytes;
|
2011-05-03 20:24:50 +00:00
|
|
|
psyc_setParseBuffer(&parsers[i], psyc_newString(parsebuf, contbytes + nbytes));
|
2011-04-27 17:18:17 +00:00
|
|
|
contbytes = 0;
|
2011-04-29 01:20:00 +00:00
|
|
|
oper = 0;
|
|
|
|
name.length = 0;
|
|
|
|
value.length = 0;
|
2011-04-27 17:18:17 +00:00
|
|
|
|
2011-05-03 23:00:35 +00:00
|
|
|
do
|
|
|
|
{
|
2011-04-27 17:18:17 +00:00
|
|
|
ret = psyc_parse(&parsers[i], &oper, &name, &value);
|
2011-05-06 11:32:12 +00:00
|
|
|
if (verbose >= 2)
|
2011-04-30 15:07:01 +00:00
|
|
|
printf("# ret = %d\n", ret);
|
2011-05-03 20:24:50 +00:00
|
|
|
|
2011-04-27 17:18:17 +00:00
|
|
|
switch (ret) {
|
|
|
|
case PSYC_PARSE_ROUTING:
|
2011-04-29 01:20:00 +00:00
|
|
|
assert(packets[i].routing.lines < ROUTING_LINES);
|
|
|
|
mod = &(packets[i].routing.modifiers[packets[i].routing.lines]);
|
|
|
|
pname = &mod->name;
|
|
|
|
pvalue = &mod->value;
|
|
|
|
mod->flag = PSYC_MODIFIER_ROUTING;
|
|
|
|
packets[i].routing.lines++;
|
|
|
|
break;
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-04-27 17:18:17 +00:00
|
|
|
case PSYC_PARSE_ENTITY_INCOMPLETE:
|
2011-04-29 01:20:00 +00:00
|
|
|
case PSYC_PARSE_ENTITY:
|
|
|
|
assert(packets[i].entity.lines < ENTITY_LINES);
|
|
|
|
mod = &(packets[i].entity.modifiers[packets[i].entity.lines]);
|
|
|
|
pname = &mod->name;
|
|
|
|
pvalue = &mod->value;
|
2011-05-03 23:00:35 +00:00
|
|
|
|
|
|
|
if (ret == PSYC_PARSE_ENTITY)
|
|
|
|
{
|
2011-04-29 01:20:00 +00:00
|
|
|
packets[i].entity.lines++;
|
2011-05-04 15:59:51 +00:00
|
|
|
mod->flag = psyc_isParseValueLengthFound(&parsers[i]) ?
|
2011-05-03 23:00:35 +00:00
|
|
|
PSYC_MODIFIER_NEED_LENGTH : PSYC_MODIFIER_NO_LENGTH;
|
2011-04-29 01:20:00 +00:00
|
|
|
}
|
|
|
|
break;
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-04-29 01:20:00 +00:00
|
|
|
case PSYC_PARSE_BODY_INCOMPLETE:
|
2011-04-27 17:18:17 +00:00
|
|
|
case PSYC_PARSE_BODY:
|
2011-04-29 01:20:00 +00:00
|
|
|
pname = &(packets[i].method);
|
|
|
|
pvalue = &(packets[i].data);
|
|
|
|
break;
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-04-29 01:20:00 +00:00
|
|
|
case PSYC_PARSE_COMPLETE:
|
2011-05-05 22:15:37 +00:00
|
|
|
if (verbose)
|
|
|
|
printf("# Done parsing.\n");
|
|
|
|
else if (progress)
|
|
|
|
write(1, ".", 1);
|
|
|
|
if (!parse_multiple) // parse multiple packets?
|
|
|
|
ret = -1;
|
|
|
|
|
2011-05-03 23:54:27 +00:00
|
|
|
packets[i].flag = psyc_isParseContentLengthFound(&parsers[i]) ?
|
2011-05-03 23:00:35 +00:00
|
|
|
PSYC_PACKET_NEED_LENGTH : PSYC_PACKET_NO_LENGTH;
|
|
|
|
|
2011-05-04 13:41:35 +00:00
|
|
|
if (routing_only)
|
2011-05-05 22:15:37 +00:00
|
|
|
{
|
|
|
|
packets[i].content = packets[i].data;
|
|
|
|
resetString(&packets[i].data, 0);
|
|
|
|
}
|
2011-05-04 13:41:35 +00:00
|
|
|
|
2011-04-29 01:20:00 +00:00
|
|
|
psyc_setPacketLength(&packets[i]);
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-05-04 13:41:35 +00:00
|
|
|
if (psyc_render(&packets[i], sendbuf, SEND_BUF_SIZE) == PSYC_RENDER_SUCCESS)
|
|
|
|
{
|
|
|
|
if (send(i, sendbuf, packets[i].length, 0) == -1)
|
2011-05-05 22:15:37 +00:00
|
|
|
{
|
2011-05-04 13:41:35 +00:00
|
|
|
perror("send error");
|
2011-05-05 22:15:37 +00:00
|
|
|
ret = -1;
|
|
|
|
}
|
2011-05-04 13:41:35 +00:00
|
|
|
}
|
|
|
|
else
|
2011-05-05 22:15:37 +00:00
|
|
|
{
|
2011-05-04 13:41:35 +00:00
|
|
|
perror("render error");
|
2011-05-05 22:15:37 +00:00
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset packet
|
|
|
|
packets[i].routingLength = 0;
|
|
|
|
packets[i].contentLength = 0;
|
|
|
|
packets[i].length = 0;
|
|
|
|
packets[i].flag = 0;
|
|
|
|
|
|
|
|
for (j = 0; j < packets[i].routing.lines; j++)
|
|
|
|
{
|
|
|
|
resetString(&packets[i].routing.modifiers[j].name, 1);
|
|
|
|
resetString(&packets[i].routing.modifiers[j].value, 1);
|
|
|
|
}
|
|
|
|
packets[i].routing.lines = 0;
|
|
|
|
|
|
|
|
if (routing_only)
|
|
|
|
{
|
|
|
|
resetString(&packets[i].content, 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (j = 0; j < packets[i].entity.lines; j++)
|
|
|
|
{
|
|
|
|
resetString(&packets[i].entity.modifiers[j].name, 1);
|
|
|
|
resetString(&packets[i].entity.modifiers[j].value, 1);
|
|
|
|
}
|
|
|
|
packets[i].entity.lines = 0;
|
|
|
|
|
|
|
|
resetString(&packets[i].method, 1);
|
|
|
|
resetString(&packets[i].data, 1);
|
|
|
|
}
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-04-29 01:20:00 +00:00
|
|
|
break;
|
2011-05-05 22:15:37 +00:00
|
|
|
|
2011-04-29 01:20:00 +00:00
|
|
|
case PSYC_PARSE_INSUFFICIENT:
|
2011-05-06 11:32:12 +00:00
|
|
|
if (verbose >= 2)
|
2011-04-29 14:02:38 +00:00
|
|
|
printf("# Insufficient data.\n");
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-05-03 23:54:27 +00:00
|
|
|
contbytes = psyc_getParseRemainingLength(&parsers[i]);
|
2011-05-03 23:00:35 +00:00
|
|
|
|
|
|
|
if (contbytes > 0) // copy end of parsebuf before start of recvbuf
|
|
|
|
{
|
2011-04-29 01:20:00 +00:00
|
|
|
assert(recvbuf - contbytes >= buf); // make sure it's still in the buffer
|
2011-05-05 22:15:37 +00:00
|
|
|
memmove(recvbuf - contbytes, psyc_getParseRemainingBuffer(&parsers[i]), contbytes);
|
2011-04-29 01:20:00 +00:00
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
break;
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-04-29 01:20:00 +00:00
|
|
|
default:
|
|
|
|
printf("# Error while parsing: %i\n", ret);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
2011-05-03 23:00:35 +00:00
|
|
|
switch (ret)
|
|
|
|
{
|
2011-04-29 01:20:00 +00:00
|
|
|
case PSYC_PARSE_ENTITY_INCOMPLETE:
|
2011-04-27 17:18:17 +00:00
|
|
|
case PSYC_PARSE_BODY_INCOMPLETE:
|
2011-04-29 01:20:00 +00:00
|
|
|
ret = 0;
|
|
|
|
case PSYC_PARSE_ENTITY:
|
|
|
|
case PSYC_PARSE_ROUTING:
|
|
|
|
case PSYC_PARSE_BODY:
|
2011-05-03 23:00:35 +00:00
|
|
|
if (oper)
|
|
|
|
{
|
2011-04-29 01:20:00 +00:00
|
|
|
mod->oper = oper;
|
2011-05-06 11:32:12 +00:00
|
|
|
if (verbose >= 2)
|
2011-04-29 14:02:38 +00:00
|
|
|
printf("%c", oper);
|
2011-04-29 01:20:00 +00:00
|
|
|
}
|
|
|
|
|
2011-05-03 23:00:35 +00:00
|
|
|
if (name.length)
|
|
|
|
{
|
2011-04-29 01:20:00 +00:00
|
|
|
pname->ptr = malloc(name.length);
|
|
|
|
pname->length = name.length;
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-04-29 01:20:00 +00:00
|
|
|
assert(pname->ptr != NULL);
|
|
|
|
memcpy((void*)pname->ptr, name.ptr, name.length);
|
|
|
|
name.length = 0;
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-05-06 11:32:12 +00:00
|
|
|
if (verbose >= 2)
|
2011-04-29 14:02:38 +00:00
|
|
|
printf("%.*s = ", (int)pname->length, pname->ptr);
|
2011-04-29 01:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value.length) {
|
2011-05-05 22:15:37 +00:00
|
|
|
if (!pvalue->length)
|
2011-04-29 01:20:00 +00:00
|
|
|
pvalue->ptr = malloc(parsers[i].valueLength ? parsers[i].valueLength : value.length);
|
|
|
|
assert(pvalue->ptr != NULL);
|
|
|
|
memcpy((void*)pvalue->ptr + pvalue->length, value.ptr, value.length);
|
|
|
|
pvalue->length += value.length;
|
|
|
|
value.length = 0;
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-05-06 11:32:12 +00:00
|
|
|
if (verbose >= 2)
|
2011-05-03 23:00:35 +00:00
|
|
|
{
|
2011-05-04 13:41:35 +00:00
|
|
|
printf("[%.*s]", (int)pvalue->length, pvalue->ptr);
|
2011-04-29 01:20:00 +00:00
|
|
|
if (parsers[i].valueLength > pvalue->length)
|
2011-04-29 14:02:38 +00:00
|
|
|
printf("...");
|
|
|
|
printf("\n");
|
2011-04-29 01:20:00 +00:00
|
|
|
}
|
2011-04-27 17:18:17 +00:00
|
|
|
}
|
2011-05-03 23:00:35 +00:00
|
|
|
else if (verbose)
|
|
|
|
printf("\n");
|
2011-04-29 01:20:00 +00:00
|
|
|
|
2011-05-06 11:32:12 +00:00
|
|
|
if (verbose >= 3)
|
2011-05-03 23:00:35 +00:00
|
|
|
printf("\t\t\t\t\t\t\t\t# n:%ld v:%ld c:%ld r:%ld\n",
|
|
|
|
pname->length, pvalue->length,
|
|
|
|
parsers[i].contentParsed, parsers[i].routingLength);
|
2011-04-29 15:01:54 +00:00
|
|
|
}
|
2011-04-29 14:02:38 +00:00
|
|
|
|
2011-05-03 23:00:35 +00:00
|
|
|
switch (ret)
|
|
|
|
{
|
2011-04-29 15:01:54 +00:00
|
|
|
case PSYC_PARSE_ROUTING:
|
|
|
|
case PSYC_PARSE_ENTITY:
|
2011-05-03 23:00:35 +00:00
|
|
|
if (pname->length >= 5 && memcmp(pname->ptr, "_list", 5) == 0)
|
|
|
|
{
|
2011-05-06 11:32:12 +00:00
|
|
|
if (verbose >= 2)
|
2011-04-29 14:02:38 +00:00
|
|
|
printf("## LIST START\n");
|
2011-05-03 20:24:50 +00:00
|
|
|
|
2011-04-27 17:18:17 +00:00
|
|
|
psyc_initParseListState(&listState);
|
2011-05-03 20:24:50 +00:00
|
|
|
psyc_setParseListBuffer(&listState, *pvalue);
|
|
|
|
|
2011-05-03 23:00:35 +00:00
|
|
|
do
|
|
|
|
{
|
2011-04-29 15:01:54 +00:00
|
|
|
retl = psyc_parseList(&listState, pname, pvalue, &elem);
|
2011-05-03 23:00:35 +00:00
|
|
|
switch (retl)
|
|
|
|
{
|
2011-04-27 17:18:17 +00:00
|
|
|
case PSYC_PARSE_LIST_END:
|
|
|
|
retl = 0;
|
|
|
|
case PSYC_PARSE_LIST_ELEM:
|
2011-05-06 11:32:12 +00:00
|
|
|
if (verbose >= 2)
|
2011-05-03 23:00:35 +00:00
|
|
|
{
|
2011-04-29 14:02:38 +00:00
|
|
|
printf("|%.*s\n", (int)elem.length, elem.ptr);
|
2011-04-27 17:18:17 +00:00
|
|
|
if (ret == PSYC_PARSE_LIST_END)
|
2011-04-29 14:02:38 +00:00
|
|
|
printf("## LIST END");
|
2011-04-27 17:18:17 +00:00
|
|
|
}
|
|
|
|
break;
|
2011-05-03 23:00:35 +00:00
|
|
|
|
2011-04-27 17:18:17 +00:00
|
|
|
default:
|
|
|
|
printf("# Error while parsing list: %i\n", ret);
|
|
|
|
ret = retl = -1;
|
|
|
|
}
|
2011-05-03 23:00:35 +00:00
|
|
|
}
|
|
|
|
while (retl > 0);
|
2011-04-27 17:18:17 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-03 23:00:35 +00:00
|
|
|
}
|
|
|
|
while (ret > 0);
|
|
|
|
|
2011-05-05 22:15:37 +00:00
|
|
|
if (progress)
|
|
|
|
write(1, " ", 1);
|
|
|
|
|
|
|
|
if (stats)
|
|
|
|
gettimeofday(&end[i], NULL);
|
|
|
|
|
2011-05-03 23:00:35 +00:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2011-05-06 11:32:12 +00:00
|
|
|
if (verbose)
|
|
|
|
printf("# Closing connection: %i\n", i);
|
2011-04-27 17:18:17 +00:00
|
|
|
close(i); // bye!
|
|
|
|
FD_CLR(i, &master); // remove from master set
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // END handle data from client
|
|
|
|
} // END got new incoming connection
|
|
|
|
} // END looping through file descriptors
|
|
|
|
} // END for(;;)--and you thought it would never end!
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|