1 /*
2  * ffproxy (c) 2002, 2003 Niklas Olmes <niklas@noxa.de>
3  * http://faith.eu.org
4  *
5  * $Id: msg.c,v 2.1 2004/12/31 08:59:15 niklas Exp $
6  *
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc., 675
19  * Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 #include "configure.h"
23 #ifdef HAVE_SYS_TYPES_H
24 # include <sys/types.h>
25 #endif
26 
27 #include <stdio.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
31 
32 #include "req.h"
33 #include "dbs.h"
34 #include "msg.h"
35 #include "poll.h"
36 
37 void
err_msg(int s,struct req * r,int m)38 err_msg(int s, struct req * r, int m)
39 {
40 	char            msg[8192];
41 	char           *p;
42 	size_t          i;
43 	int             j;
44 
45 	p = NULL;
46 
47 	switch (m) {
48 	case E_INV:
49 		p = e_inv;
50 		break;
51 	case E_RES:
52 		p = e_res;
53 		break;
54 	case E_CON:
55 		p = e_con;
56 		break;
57 	case E_POST:
58 		p = e_post;
59 		break;
60 	case E_FIL:
61 		p = e_fil;
62 		break;
63 	}
64 
65 	*msg = '\0';
66 	i = 0;
67 	while (p != NULL && *p != '\0' && i < sizeof(msg) - 1)
68 		if (*p == '$') {
69 			switch (*(p + 1)) {
70 			case 'u':
71 				j = 0;
72 				while (i < sizeof(msg) - 1 && r->url[j] != '\0')
73 					msg[i++] = r->url[j++];
74 				p += 2;
75 				break;
76 			case 'h':
77 				j = 0;
78 				while (i < sizeof(msg) - 1 && r->host[j] != '\0')
79 					msg[i++] = r->host[j++];
80 				p += 2;
81 				break;
82 			case 'c':
83 				j = 0;
84 				while (i < sizeof(msg) - 1 && r->cl->name[j] != '\0')
85 					msg[i++] = r->cl->name[j++];
86 				p += 2;
87 				break;
88 			default:
89 				msg[i++] = *(p++);
90 				break;
91 			}
92 		} else
93 			msg[i++] = *(p++);
94 
95 	msg[i] = '\0';
96 
97 	if (i > 0 && my_poll(s, OUT))
98 		(void) write(s, msg, i - 1);
99 }
100