1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright (c) 1996-1998,2001 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate * All rights reserved.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*7c478bd9Sstevel@tonic-gate
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate * Protocol interpreter for the Hypertext Transfer Protocol (HTTP)
31*7c478bd9Sstevel@tonic-gate *
32*7c478bd9Sstevel@tonic-gate * Relevant standards:
33*7c478bd9Sstevel@tonic-gate * Berners-Lee, T., et al: Hypertext Transfer Protocol -- HTTP/1.0.
34*7c478bd9Sstevel@tonic-gate * RFC 1945, May 1996
35*7c478bd9Sstevel@tonic-gate * Fielding, R., et al: Hypertext Transfer Protocol -- HTTP/1.1.
36*7c478bd9Sstevel@tonic-gate * RFC 2068, June 1999
37*7c478bd9Sstevel@tonic-gate */
38*7c478bd9Sstevel@tonic-gate
39*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
40*7c478bd9Sstevel@tonic-gate #include <stdio.h>
41*7c478bd9Sstevel@tonic-gate #include <string.h>
42*7c478bd9Sstevel@tonic-gate #include <ctype.h>
43*7c478bd9Sstevel@tonic-gate #include "snoop.h"
44*7c478bd9Sstevel@tonic-gate
45*7c478bd9Sstevel@tonic-gate #define CR 13 /* "carriage return" character */
46*7c478bd9Sstevel@tonic-gate #define LF 10 /* "line feed" character */
47*7c478bd9Sstevel@tonic-gate
48*7c478bd9Sstevel@tonic-gate /*
49*7c478bd9Sstevel@tonic-gate * Summary lines: packet contents starting with less than MINCHARS
50*7c478bd9Sstevel@tonic-gate * printable characters will not be printed. MAXCHARS is the maximum
51*7c478bd9Sstevel@tonic-gate * number of characters printed.
52*7c478bd9Sstevel@tonic-gate * Detail lines: NLINES is the maximum number of content lines to print
53*7c478bd9Sstevel@tonic-gate */
54*7c478bd9Sstevel@tonic-gate #define MINCHARS 10
55*7c478bd9Sstevel@tonic-gate #define MAXCHARS 80
56*7c478bd9Sstevel@tonic-gate #define NLINES 5
57*7c478bd9Sstevel@tonic-gate
58*7c478bd9Sstevel@tonic-gate #define MIN(a, b) (((a) < (b)) ? (a) : (b))
59*7c478bd9Sstevel@tonic-gate
60*7c478bd9Sstevel@tonic-gate static int printable(const char *line, const char *endp);
61*7c478bd9Sstevel@tonic-gate
62*7c478bd9Sstevel@tonic-gate int
interpret_http(int flags,char * line,int fraglen)63*7c478bd9Sstevel@tonic-gate interpret_http(int flags, char *line, int fraglen)
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate char *p, *q, *endp;
66*7c478bd9Sstevel@tonic-gate int c;
67*7c478bd9Sstevel@tonic-gate int lineno;
68*7c478bd9Sstevel@tonic-gate
69*7c478bd9Sstevel@tonic-gate endp = line + fraglen;
70*7c478bd9Sstevel@tonic-gate
71*7c478bd9Sstevel@tonic-gate if (flags & F_SUM) {
72*7c478bd9Sstevel@tonic-gate c = printable(line, endp - 1);
73*7c478bd9Sstevel@tonic-gate if (c < MINCHARS) {
74*7c478bd9Sstevel@tonic-gate (void) snprintf(get_sum_line(), MAXLINE,
75*7c478bd9Sstevel@tonic-gate "HTTP (body)");
76*7c478bd9Sstevel@tonic-gate } else {
77*7c478bd9Sstevel@tonic-gate (void) snprintf(get_sum_line(), MAXLINE,
78*7c478bd9Sstevel@tonic-gate "HTTP %.*s", MIN(c, MAXCHARS), line);
79*7c478bd9Sstevel@tonic-gate }
80*7c478bd9Sstevel@tonic-gate }
81*7c478bd9Sstevel@tonic-gate
82*7c478bd9Sstevel@tonic-gate if (flags & F_DTAIL) {
83*7c478bd9Sstevel@tonic-gate show_header("HTTP: ", "HyperText Transfer Protocol", fraglen);
84*7c478bd9Sstevel@tonic-gate show_space();
85*7c478bd9Sstevel@tonic-gate
86*7c478bd9Sstevel@tonic-gate lineno = 0;
87*7c478bd9Sstevel@tonic-gate for (p = line; p < endp && lineno < NLINES; p = q + 1) {
88*7c478bd9Sstevel@tonic-gate c = printable(p, endp - 1);
89*7c478bd9Sstevel@tonic-gate
90*7c478bd9Sstevel@tonic-gate /* stop if no printables, except if at line end */
91*7c478bd9Sstevel@tonic-gate if (c == 0 && *p != CR && *p != LF)
92*7c478bd9Sstevel@tonic-gate break;
93*7c478bd9Sstevel@tonic-gate
94*7c478bd9Sstevel@tonic-gate /*
95*7c478bd9Sstevel@tonic-gate * A line may be terminated either by an CR LF sequence
96*7c478bd9Sstevel@tonic-gate * (DOS, Mac), or by LF alone
97*7c478bd9Sstevel@tonic-gate */
98*7c478bd9Sstevel@tonic-gate
99*7c478bd9Sstevel@tonic-gate q = memchr(p, CR, (endp - p));
100*7c478bd9Sstevel@tonic-gate if (q != NULL) {
101*7c478bd9Sstevel@tonic-gate if (q < endp - 1 && q[1] == LF)
102*7c478bd9Sstevel@tonic-gate ++q; /* ignore subsequent LF character */
103*7c478bd9Sstevel@tonic-gate } else {
104*7c478bd9Sstevel@tonic-gate q = memchr(p, LF, (endp - p));
105*7c478bd9Sstevel@tonic-gate /* no CR/LF: use end of buffer */
106*7c478bd9Sstevel@tonic-gate if (q == NULL)
107*7c478bd9Sstevel@tonic-gate q = endp - 1;
108*7c478bd9Sstevel@tonic-gate }
109*7c478bd9Sstevel@tonic-gate
110*7c478bd9Sstevel@tonic-gate /* truncate lines containing non-printable characters */
111*7c478bd9Sstevel@tonic-gate (void) snprintf(get_line(0, c), get_line_remain(),
112*7c478bd9Sstevel@tonic-gate "%.*s", c, p);
113*7c478bd9Sstevel@tonic-gate ++lineno;
114*7c478bd9Sstevel@tonic-gate }
115*7c478bd9Sstevel@tonic-gate
116*7c478bd9Sstevel@tonic-gate if (p < endp) /* there was more data to be printed */
117*7c478bd9Sstevel@tonic-gate (void) snprintf(get_line(0, 5), get_line_remain(),
118*7c478bd9Sstevel@tonic-gate "[...]");
119*7c478bd9Sstevel@tonic-gate
120*7c478bd9Sstevel@tonic-gate show_space();
121*7c478bd9Sstevel@tonic-gate }
122*7c478bd9Sstevel@tonic-gate
123*7c478bd9Sstevel@tonic-gate return (fraglen);
124*7c478bd9Sstevel@tonic-gate }
125*7c478bd9Sstevel@tonic-gate
126*7c478bd9Sstevel@tonic-gate /*
127*7c478bd9Sstevel@tonic-gate * Return the length of the initial string starting with "startp" and
128*7c478bd9Sstevel@tonic-gate * ending with "endp" (inclusively) consisting only of printable
129*7c478bd9Sstevel@tonic-gate * characters.
130*7c478bd9Sstevel@tonic-gate */
131*7c478bd9Sstevel@tonic-gate
132*7c478bd9Sstevel@tonic-gate static int
printable(const char * startp,const char * endp)133*7c478bd9Sstevel@tonic-gate printable(const char *startp, const char *endp)
134*7c478bd9Sstevel@tonic-gate {
135*7c478bd9Sstevel@tonic-gate const char *p = startp;
136*7c478bd9Sstevel@tonic-gate
137*7c478bd9Sstevel@tonic-gate while (p <= endp && (isprint(*p) || *p == '\t'))
138*7c478bd9Sstevel@tonic-gate p++;
139*7c478bd9Sstevel@tonic-gate
140*7c478bd9Sstevel@tonic-gate return (p - startp);
141*7c478bd9Sstevel@tonic-gate }
142