1 /**
2  * Copyright 2006 Christian Liesch
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /**
18  * @file
19  *
20  * @Author christian liesch <liesch@gmx.ch>
21  *
22  * Implementation of the HTTP Test Tool simple appender.
23  */
24 
25 /************************************************************************
26  * Includes
27  ***********************************************************************/
28 #include <config.h>
29 #include <apr.h>
30 #include <apr_strings.h>
31 #include <apr_file_io.h>
32 #include <apr_env.h>
33 
34 #include <apr.h>
35 #include <apr_lib.h>
36 #include <apr_errno.h>
37 #include <apr_strings.h>
38 #include <apr_portable.h>
39 #include <apr_hash.h>
40 #include <apr_base64.h>
41 #include <apr_hooks.h>
42 #include <apr_env.h>
43 
44 #include "defines.h"
45 #include "util.h"
46 #include "replacer.h"
47 #include "regex.h"
48 #include "file.h"
49 #include "transport.h"
50 #include "socket.h"
51 #include "worker.h"
52 
53 #include "appender.h"
54 
55 
56 /************************************************************************
57  * Definitions
58  ***********************************************************************/
59 typedef struct appender_simple_s {
60   apr_file_t *out;
61 } appender_simple_t;
62 
63 /************************************************************************
64  * Forward declaration
65  ***********************************************************************/
66 void appender_simple_printer(appender_t *appender, int mode, const char *pos,
67                              int thread, int group, char dir, const char *custom,
68                              const char *buf, apr_size_t len);
69 
70 /************************************************************************
71  * Implementation
72  ***********************************************************************/
73 
74 /**
75  * Constructor for simple appender
76  * @param pool IN pool
77  * @param out IN output file
78  * @return appender
79  */
appender_simple_new(apr_pool_t * pool,apr_file_t * out)80 appender_t *appender_simple_new(apr_pool_t *pool, apr_file_t *out) {
81   appender_t *appender;
82   appender_simple_t *simple = apr_pcalloc(pool, sizeof(*simple));
83   simple->out = out;
84   appender = appender_new(pool, appender_simple_printer, simple);
85 
86   return appender;
87 }
88 
89 /**
90  * Simple appender printer
91  * @param appender IN appender instance
92  * @param mode IN mode
93  * @param thread IN thread id
94  * |@param group IN group id
95  * @param dir IN >,<,+,=
96  * @param custom IN custom string
97  * @param buf IN buffer to print
98  * @param len IN buffer len
99  */
appender_simple_printer(appender_t * appender,int mode,const char * pos,int thread,int group,char dir,const char * custom,const char * buf,apr_size_t len)100 void appender_simple_printer(appender_t *appender, int mode, const char *pos,
101                              int thread, int group, char dir, const char *custom,
102                              const char *buf, apr_size_t len) {
103   appender_simple_t *simple = appender_get_user_data(appender);
104 
105   if (!buf) {
106     buf = "";
107     len = strlen(buf);
108   }
109 
110 
111   if (simple->out) {
112     apr_size_t i = 0;
113     apr_size_t j = 0;
114     do {
115       for (; i < len && buf[i] != '\n'; i++);
116       ++i;
117       apr_file_printf(simple->out, "\n%c:", dir);
118 
119       for (; j < i; j++) {
120         if ((unsigned char)buf[j] == '\n') {
121         }
122         else if ((unsigned char)buf[j] == '\r') {
123         }
124         else if ((unsigned char)buf[j] == '\0') {
125         }
126         else if ((unsigned char)buf[j] < 0x20) {
127           apr_file_putc('.', simple->out);
128         }
129         else {
130           apr_file_putc(buf[j], simple->out);
131         }
132       }
133       apr_file_flush(simple->out);
134     } while (i < len);
135   }
136 }
137 
138