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