/* $NetBSD: quip_server.c,v 1.7 2024/10/04 15:37:00 rillig Exp $ */
/* $KAME: quip_server.c,v 1.6 2001/08/20 06:41:32 kjc Exp $ */
/*
* Copyright (C) 1999-2000
* Sony Computer Science Laboratories, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
*
* THIS SOFTWARE IS PROVIDED BY SONY CSL 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 SONY CSL 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.
*/
int
quip_input(FILE *fp)
{
char request[REQ_MAXSIZE], result[RES_MAXSIZE], body[BODY_MAXSIZE],
w[REQ_MAXSIZE], *cp, *query;
int n = 0;
while (1) {
if (fgets(request, REQ_MAXSIZE, fp) == NULL) /* EOF */
return (-1);
/* skip preceding blank lines */
if (request[0] == '\n')
continue;
break;
}
/* remove trailing newline and white space */
if ((cp = strrchr(request, '\n')) != NULL) {
*cp-- = '\0';
while (*cp == ' ' || *cp == '\t')
*cp-- = '\0';
}
body[0] = '\0';
cp = request;
if (!next_word(&cp, w)) {
snprintf(result, sizeof(result), "400 Bad request\n");
goto done;
}
if (EQUAL(w, "GET")) {
if (!next_word(&cp, w)) {
snprintf(result, sizeof(result), "400 Bad request\n");
goto done;
}
if ((query = strchr(w, '?')) != NULL) {
/* request has a query string */
*query = '\0';
query++;
}
if (EQUAL(w, "list")) {
n = query_list(w, query, body, BODY_MAXSIZE);
} else if (EQUAL(w, "handle-to-name")) {
n = query_handle2name(w, query, body, BODY_MAXSIZE);
} else if (EQUAL(w, "qdisc")) {
n = query_qdisc(w, query, body, BODY_MAXSIZE);
} else if (EQUAL(w, "filter")) {
n = query_filterspec(w, query, body, BODY_MAXSIZE);
} else {
snprintf(result, sizeof(result), "400 Bad request\n");
goto done;
}
} else {
snprintf(result, sizeof(result), "400 Bad request\n");
goto done;
}
if (n == 0) {
snprintf(result, sizeof(result), "204 No content\n");
} else if (n < 0) {
snprintf(result, sizeof(result), "400 Bad request\n");
} else {
snprintf(result, sizeof(result), "200 OK\nContent-Length:%d\n", n);
}
done:
/* send a result line and a blank line */
if (fputs ("QUIP/1.0 ", fp) != 0 ||
fputs(result, fp) != 0 || fputs("\n", fp) != 0)
return (-1);
/* send message body */
if (fputs(body, fp) != 0)
return (-1);
return (0);
}
/*
* Skip leading blanks, then copy next word (delimited by blank or zero, but
* no longer than 63 bytes) into buffer b, set scan pointer to following
* non-blank (or end of string), and return 1. If there is no non-blank text,
* set scan ptr to point to 0 byte and return 0.
*/
static int
next_word(char **cpp, char *b)
{
char *tp;
int L;
/*
* string_match compares 2 strings and returns 1 when s1 matches s2.
* s1: possibly includes wildcards, "*".
* s2: must be a full string (should not include "*").
*/
static int
string_match(const char *s1, const char *s2)
{
char *ap, *next, sub[256];
int prefixlen, sublen;
/* if there's no wild card, compare full string */
if ((ap = strchr(s1, '*')) == NULL)
return (strcmp(s1, s2) == 0);
/*
* if there is another wildcard in the rest of the string,
* compare the substring between the 2 wildcards.
*/
while ((next = strchr(ap + 1, '*')) != NULL) {
sublen = next - ap - 1;
strncpy(sub, ap+1, sublen);
sub[sublen] = '\0';
if ((s2 = strstr(s2, sub)) == NULL)
return (0);
s2 += sublen;
ap = next;
}
/* no more wildcard, compare the rest of the string */
return (strcmp(ap+1, s2+strlen(s2)-strlen(ap+1)) == 0);
}