00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include "HdrHeap.h"
00047 #include "MIME.h"
00048 #include "HTTP.h"
00049 #include "Tokenizer.h"
00050 #include "Diags.h"
00051 #include <stdio.h>
00052 #include <errno.h>
00053 #include <sys/types.h>
00054 #include <sys/stat.h>
00055 #include <fcntl.h>
00056 #include <unistd.h>
00057
00058 void *low_load_addr = NULL;
00059 void *high_load_addr = NULL;
00060 int heap_load_size = 0;
00061 int marshalled = 0;
00062
00063
00064
00065 enum hdr_type
00066 {
00067 UNKNOWN_HDR,
00068 REQUEST_HDR,
00069 RESPONSE_HDR,
00070 HTTP_INFO_HDR,
00071 RAW_MBUFFER
00072 };
00073
00074 char *
00075 load_string(const char *s, int len, int offset)
00076 {
00077
00078 const char *copy_from;
00079 if (marshalled == 0 && s > low_load_addr && s + len < high_load_addr) {
00080 copy_from = s + offset;
00081 } else if (marshalled && ((unsigned int) s) + len < (unsigned int) heap_load_size) {
00082 copy_from = s + offset;
00083 } else {
00084 copy_from = "<BAD>";
00085 len = strlen(copy_from);
00086 }
00087 char *r = (char *)ats_malloc(len + 1);
00088 memcpy(r, copy_from, len);
00089 r[len] = '\0';
00090 return r;
00091 }
00092
00093
00094 void
00095 process_http_hdr_impl(HdrHeapObjImpl * obj, int offset)
00096 {
00097 char *s;
00098 HTTPHdrImpl *hhdr = (HTTPHdrImpl *) obj;
00099
00100 if (hhdr->m_polarity == HTTP_TYPE_REQUEST) {
00101 printf(" is a request hdr\n");
00102 s = load_string(hhdr->u.req.m_ptr_method, hhdr->u.req.m_len_method, offset);
00103 printf(" method: %s\n", s);
00104 ats_free(s);
00105 } else if (hhdr->m_polarity == HTTP_TYPE_RESPONSE) {
00106 printf(" is a response hdr\n");
00107 printf(" status code: %d\n", (int) hhdr->u.resp.m_status);
00108 s = load_string(hhdr->u.resp.m_ptr_reason, hhdr->u.resp.m_len_reason, offset);
00109 printf(" method: %s\n", s);
00110 ats_free(s);
00111 }
00112 }
00113
00114 void
00115 process_mime_block_impl(MIMEFieldBlockImpl * mblock, int offset)
00116 {
00117
00118 printf(" Processing mime_flock_impl - freetop %d\n", mblock->m_freetop);
00119 int freetop;
00120 if (mblock->m_freetop <= MIME_FIELD_BLOCK_SLOTS) {
00121 freetop = mblock->m_freetop;
00122 } else {
00123 freetop = MIME_FIELD_BLOCK_SLOTS;
00124 }
00125
00126 char *n, *v;
00127 for (unsigned int i = 0; i < freetop; i++) {
00128 MIMEField *f = &mblock->m_field_slots[i];
00129 if (hdrtoken_is_valid_wks_idx(f->m_wks_idx)) {
00130 n = ats_strdup(hdrtoken_index_to_wks(f->m_wks_idx));
00131 } else {
00132 n = load_string(f->m_ptr_name, f->m_len_name, offset);
00133 }
00134 v = load_string(f->m_ptr_value, f->m_len_value, offset);
00135 printf(" (%d) %s: %s\n", i, n, v);
00136 ats_free(n);
00137 ats_free(v);
00138 }
00139 }
00140
00141 void
00142 process_mime_hdr_impl(HdrHeapObjImpl * obj, int offset)
00143 {
00144 MIMEHdrImpl *mhdr = (MIMEHdrImpl *) obj;
00145
00146 process_mime_block_impl(&mhdr->m_first_fblock, offset);
00147 }
00148
00149
00150 void
00151 loop_over_heap_objs(HdrHeap * hdr_heap, int offset)
00152 {
00153
00154 char *buffer_end;
00155
00156 printf("Looping over HdrHeap objects @ 0x%X\n", hdr_heap);
00157
00158 if (hdr_heap->m_magic == HDR_BUF_MAGIC_MARSHALED) {
00159 printf(" marshalled heap - size %d\n", hdr_heap->m_size);
00160 hdr_heap->m_data_start = ((char *) hdr_heap) + ROUND(sizeof(HdrHeap), HDR_PTR_SIZE);
00161 hdr_heap->m_free_start = ((char *) hdr_heap) + hdr_heap->m_size;
00162 } else {
00163 buffer_end = hdr_heap->m_free_start;
00164 }
00165
00166 printf(" heap len is %d bytes\n", hdr_heap->m_free_start - hdr_heap->m_data_start);
00167
00168 char *obj_data = hdr_heap->m_data_start;
00169 while (obj_data < hdr_heap->m_free_start) {
00170 HdrHeapObjImpl *obj = (HdrHeapObjImpl *) obj_data;
00171
00172 switch (obj->m_type) {
00173 case HDR_HEAP_OBJ_HTTP_HEADER:
00174 printf(" HDR_HEAP_OBJ_HTTP_HEADER %d bytes\n", obj->m_length);
00175 process_http_hdr_impl(obj, offset);
00176 break;
00177 case HDR_HEAP_OBJ_URL:
00178 printf(" HDR_HEAP_OBJ_URL %d bytes\n", obj->m_length);
00179 break;
00180 case HDR_HEAP_OBJ_FIELD_BLOCK:
00181 printf(" HDR_HEAP_OBJ_FIELD_BLOCK %d bytes\n", obj->m_length);
00182 break;
00183 case HDR_HEAP_OBJ_MIME_HEADER:
00184 printf(" HDR_HEAP_OBJ_MIME_HEADER %d bytes\n", obj->m_length);
00185 process_mime_hdr_impl(obj, offset);
00186 break;
00187 case HDR_HEAP_OBJ_EMPTY:
00188 printf(" HDR_HEAP_OBJ_EMPTY %d bytes\n", obj->m_length);
00189 break;
00190 default:
00191 printf(" OBJ UNKONWN (%d) %d bytes\n", obj->m_type, obj->m_length);
00192 }
00193
00194 if (obj->m_length == 0) {
00195 printf(" ERROR zero length object\n");
00196 break;
00197 }
00198 obj_data = obj_data + obj->m_length;
00199 }
00200 }
00201
00202
00203 void
00204 load_buffer(int fd, hdr_type h_type)
00205 {
00206
00207 struct stat s_info;
00208
00209 if (fstat(fd, &s_info) < 0) {
00210 fprintf(stderr, "Failed to stat data file : %s\n", strerror(errno));
00211 exit(1);
00212 }
00213
00214 char *file_buf = (char *)ats_malloc(sizeof(char) * (s_info.st_size + 1));
00215 file_buf[s_info.st_size] = '\0';
00216
00217
00218
00219 int bytes_to_read = s_info.st_size;
00220 while (bytes_to_read > 0) {
00221 int done = read(fd, file_buf, bytes_to_read);
00222
00223 if (done < 0) {
00224 fprintf(stderr, "Failed to read data file : %s\n", strerror(errno));
00225 exit(1);
00226 } else if (done == 0) {
00227 fprintf(stderr, "EOF encounted\n");
00228 exit(1);
00229 }
00230
00231 bytes_to_read -= done;
00232 }
00233
00234 Tokenizer line_tok("\n");
00235 Tokenizer el_tok(" \t");
00236
00237
00238 int num_lines = line_tok.Initialize(file_buf);
00239 int num_el = el_tok.Initialize(line_tok[0]);
00240
00241 if (num_el < 3) {
00242 fprintf(stderr, "Corrupted data file\n");
00243 exit(1);
00244 }
00245
00246 void *old_addr = NULL;
00247 if (sscanf(el_tok[0], "%x:", &old_addr) != 1) {
00248 fprintf(stderr, "Corrupted data file\n");
00249 exit(1);
00250 }
00251 low_load_addr = old_addr;
00252
00253 int hdr_size;
00254 if (sscanf(el_tok[4], "%x", &hdr_size) != 1) {
00255 fprintf(stderr, "Corrupted data file\n");
00256 exit(1);
00257 }
00258 hdr_size = (num_lines * 16);
00259 heap_load_size = hdr_size;
00260
00261 char *hdr_heap = (char *)ats_malloc(hdr_size);
00262 int bytes_read = 0;
00263 int cur_line = 0;
00264
00265 while (cur_line < num_lines && bytes_read < hdr_size) {
00266
00267 int *cur_ptr;
00268 num_el = el_tok.Initialize(line_tok[cur_line]);
00269
00270 if (sscanf(el_tok[0], "%x:", &high_load_addr) != 1) {
00271 fprintf(stderr, "Corrupted data file\n");
00272 exit(1);
00273 }
00274 high_load_addr = ((char *) high_load_addr) + (num_el * 4);
00275
00276 int el;
00277 for (int i = 1; i < num_el; i++) {
00278 if (sscanf(el_tok[i], "%x", &el) != 1) {
00279 fprintf(stderr, "Corrupted data file\n");
00280 exit(1);
00281 }
00282 cur_ptr = (int *) (hdr_heap + bytes_read);
00283 *cur_ptr = el;
00284 bytes_read += 4;
00285 }
00286 cur_line++;
00287 }
00288
00289 HdrHeap *my_heap = (HdrHeap *) hdr_heap;
00290 int offset = hdr_heap - (char *) old_addr;
00291
00292
00293 if (my_heap->m_magic == HDR_BUF_MAGIC_MARSHALED) {
00294
00295
00296 marshalled = 1;
00297 offset = (int) hdr_heap;
00298 } else {
00299 my_heap->m_free_start = my_heap->m_free_start + offset;
00300 my_heap->m_data_start = my_heap->m_data_start + offset;
00301 my_heap->m_ronly_heap[0].m_heap_start = my_heap->m_ronly_heap[0].m_heap_start + offset;
00302 }
00303 loop_over_heap_objs(my_heap, offset);
00304 }
00305
00306 int
00307 main(int argc, const char *argv[])
00308 {
00309
00310 hdr_type h_type = UNKNOWN_HDR;
00311
00312 http_init();
00313 diags = new Diags(NULL, NULL);
00314 if (argc != 3) {
00315 fprintf(stderr, "Usage: %s req|res <file>\n", argv[0]);
00316 exit(1);
00317 }
00318
00319 if (strcasecmp(argv[1], "req") == 0) {
00320 h_type = REQUEST_HDR;
00321 } else if (strcasecmp(argv[1], "resp") == 0) {
00322 h_type = RESPONSE_HDR;
00323 } else if (strcasecmp(argv[1], "hinfo") == 0) {
00324 h_type = HTTP_INFO_HDR;
00325 } else if (strcasecmp(argv[1], "mbuf") == 0) {
00326 h_type = RAW_MBUFFER;
00327 } else {
00328 fprintf(stderr, "Usage: %s req|resp|hinfo|mbuf <file>\n", argv[0]);
00329 exit(1);
00330 }
00331
00332 int fd = open(argv[2], O_RDONLY);
00333
00334 if (fd < 0) {
00335 fprintf(stderr, "Could not open file %s : %s\n", argv[2], strerror(errno));
00336 exit(1);
00337 }
00338 load_buffer(fd, h_type);
00339
00340 return 0;
00341 }