• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

load_http_hdr.cc

Go to the documentation of this file.
00001 /** @file
00002 
00003   A brief file description
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022  */
00023 
00024 /****************************************************************************
00025 
00026    load_http_hdr.cc
00027 
00028    Description:
00029 
00030    Opens a file with a dbx dump of an http hdr and prints it out
00031 
00032 
00033  ****************************************************************************/
00034 
00035 #include "Marshal.h"
00036 #include "MIME.h"
00037 #include "HTTP.h"
00038 #include "Tokenizer.h"
00039 #include <stdio.h>
00040 #include <errno.h>
00041 #include <sys/types.h>
00042 #include <sys/stat.h>
00043 #include <fcntl.h>
00044 #include <unistd.h>
00045 
00046 enum hdr_type
00047 {
00048   UNKNOWN_HDR,
00049   REQUEST_HDR,
00050   RESPONSE_HDR,
00051   HTTP_INFO_HDR,
00052   RAW_MBUFFER
00053 };
00054 
00055 void walk_mime_field(MIMEField f);
00056 void walk_mstring(MBuffer * bufp, int32_t offset);
00057 void walk_mbuffer(MBuffer * bufp);
00058 void print_http_info_impl(HTTPInfo hi);
00059 void print_http_hdr_impl(HTTPHdr h);
00060 
00061 void
00062 print_hdr(HTTPHdr to_print)
00063 {
00064   char b[4096];
00065   int used, tmp, offset;
00066   int done;
00067   offset = 0;
00068   do {
00069     used = 0;
00070     tmp = offset;
00071     done = to_print.print(b, 4095, &used, &tmp);
00072     offset += used;
00073     b[used] = '\0';
00074     printf("%s", b);
00075   } while (!done);
00076 }
00077 
00078 void
00079 dump_hdr(char *mbuf, hdr_type h_type)
00080 {
00081   HTTPHdr to_dump;
00082   HTTPInfo to_dump_info;
00083 
00084   if (h_type == RESPONSE_HDR) {
00085     to_dump.locate_resp(mbuf);
00086     print_hdr(to_dump);
00087   } else if (h_type == REQUEST_HDR) {
00088     to_dump.locate_req(mbuf);
00089     print_hdr(to_dump);
00090   } else {
00091     to_dump_info.locate(mbuf);
00092 
00093     to_dump = to_dump_info.request_get();
00094     if (to_dump.valid()) {
00095       print_hdr(to_dump);
00096     } else {
00097       fprintf(stderr, "HttpInfo request invalid\n");
00098     }
00099 
00100     to_dump = to_dump_info.response_get();
00101 
00102     if (to_dump.valid()) {
00103       print_hdr(to_dump);
00104     } else {
00105       fprintf(stderr, "HttpInfo response invalid\n");
00106     }
00107   }
00108 }
00109 
00110 void
00111 load_buffer(int fd, hdr_type h_type)
00112 {
00113 
00114   struct stat s_info;
00115 
00116   if (fstat(fd, &s_info) < 0) {
00117     fprintf(stderr, "Failed to stat data file : %s\n", strerror(errno));
00118     exit(1);
00119   }
00120 
00121   char *file_buf = (char *)ats_malloc(sizeof(char) * (s_info.st_size + 1));
00122   file_buf[s_info.st_size] = '\0';
00123 
00124 
00125   // Read in the entire file
00126   int bytes_to_read = s_info.st_size;
00127   while (bytes_to_read > 0) {
00128     int done = read(fd, file_buf, bytes_to_read);
00129 
00130     if (done < 0) {
00131       fprintf(stderr, "Failed to read data file : %s\n", strerror(errno));
00132       exit(1);
00133     } else if (done == 0) {
00134       fprintf(stderr, "EOF encounted\n", strerror(errno));
00135       exit(1);
00136     }
00137 
00138     bytes_to_read -= done;
00139   }
00140 
00141   Tokenizer line_tok("\n");
00142   Tokenizer el_tok(" \t");
00143 
00144 
00145   int num_lines = line_tok.Initialize(file_buf);
00146   int num_el = el_tok.Initialize(line_tok[0]);
00147 
00148   if (num_el < 3) {
00149     fprintf(stderr, "Corrupted data file\n");
00150     exit(1);
00151   }
00152 
00153   int mbuf_size, mbuf_length;
00154   if (sscanf(el_tok[2], "%x", &mbuf_length) != 1) {
00155     fprintf(stderr, "Corrupted data file\n");
00156     exit(1);
00157   }
00158 
00159   mbuf_size = MARSHAL_DEFAULT_SIZE;
00160   while (mbuf_size < mbuf_length) {
00161     mbuf_size *= 2;
00162   }
00163 
00164   char *mbuf = (char *)ats_malloc(mbuf_size);
00165   int bytes_read = 0;
00166   int cur_line = 0;
00167 
00168   while (cur_line < num_lines && bytes_read < mbuf_size) {
00169 
00170     int *cur_ptr;
00171     num_el = el_tok.Initialize(line_tok[cur_line]);
00172 
00173     int el;
00174     for (int i = 1; i < num_el; i++) {
00175       if (sscanf(el_tok[i], "%x", &el) != 1) {
00176         fprintf(stderr, "Corrupted data file\n");
00177         exit(1);
00178       }
00179       cur_ptr = (int *) (mbuf + bytes_read);
00180       *cur_ptr = el;
00181       bytes_read += 4;
00182     }
00183     cur_line++;
00184   }
00185 
00186   if (bytes_read != mbuf_length) {
00187     fprintf(stderr, "Size mismatch: read %d  mbuf_length %d  mbuf_size %d\n", bytes_read, mbuf_length, mbuf_size);
00188 //      exit(1);
00189   }
00190 
00191   /*
00192      int raw_fd = open("foo", O_RDWR |O_CREAT | O_TRUNC );
00193      if (raw_fd > 0) {
00194      write(raw_fd, mbuf, mbuf_size);
00195      close(raw_fd);
00196      } else {
00197      perror("open: ");
00198      }
00199    */
00200 
00201   if (h_type == RAW_MBUFFER) {
00202     MBuffer m_buf_struct;
00203     memset(&m_buf_struct, 0, sizeof(MBuffer));
00204     mbuffer_set(&m_buf_struct, mbuf);
00205     m_buf_struct.m_ext_refcount = 1;
00206     m_buf_struct.m_size = bytes_read;
00207     walk_mbuffer(&m_buf_struct);
00208   } else {
00209     dump_hdr(mbuf, h_type);
00210   }
00211 }
00212 
00213 int
00214 main(int argc, const char *argv[])
00215 {
00216 
00217   hdr_type h_type = UNKNOWN_HDR;
00218 
00219   http_init();
00220   if (argc != 3) {
00221     fprintf(stderr, "Usage: %s req|res <file>\n", argv[0]);
00222     exit(1);
00223   }
00224 
00225   if (strcasecmp(argv[1], "req") == 0) {
00226     h_type = REQUEST_HDR;
00227   } else if (strcasecmp(argv[1], "resp") == 0) {
00228     h_type = RESPONSE_HDR;
00229   } else if (strcasecmp(argv[1], "hinfo") == 0) {
00230     h_type = HTTP_INFO_HDR;
00231   } else if (strcasecmp(argv[1], "mbuf") == 0) {
00232     h_type = RAW_MBUFFER;
00233   } else {
00234     fprintf(stderr, "Usage: %s req|resp|hinfo|mbuf <file>\n", argv[0]);
00235     exit(1);
00236   }
00237 
00238   int fd = open(argv[2], O_RDONLY);
00239 
00240   if (fd < 0) {
00241     fprintf(stderr, "Could not open file %s : %s\n", argv[2], strerror(errno));
00242     exit(1);
00243   }
00244   load_buffer(fd, h_type);
00245 
00246   return 0;
00247 }
00248 
00249 
00250 /*********************************************************************
00251   Code for manual groking the mbuf objects
00252 *******************************************************************/
00253 
00254 //extern const char *marshal_strs[];
00255 
00256 char *marshal_type_strs[] = {
00257   "EMPTY ",
00258   "OBJ   ",
00259   "STR   ",
00260   "URL   ",
00261   "URL_F ",
00262   "URL_H ",
00263   "M_VALS",
00264   "M_FLD ",
00265   "M_HDR ",
00266   "H_HDR ",
00267   "H_REQ ",
00268   "H_RESP",
00269   "H_INFO"
00270 };
00271 
00272 void
00273 walk_mbuffer(MBuffer * bufp)
00274 {
00275   int offset = 3;
00276   int max_offset = (*bufp->m_length) / 4;
00277 
00278   do {
00279     MObjectImpl *mo = (MObjectImpl *) mbuffer_get_obj(bufp, offset);
00280     printf("offset %.3d  m_length %.2d  m_type %s     ", offset, (int) mo->m_length, marshal_type_strs[mo->type]);
00281 
00282     switch ((int) mo->type) {
00283     case MARSHAL_MIME_FIELD:
00284       {
00285         MIMEField f(bufp, offset);
00286         walk_mime_field(f);
00287         break;
00288       }
00289     case MARSHAL_STRING:
00290       {
00291         walk_mstring(bufp, offset);
00292         printf("\n");
00293         break;
00294       }
00295     case MARSHAL_HTTP_INFO:
00296       {
00297         HTTPInfo i(bufp, offset);
00298         print_http_info_impl(i);
00299         printf("\n");
00300         break;
00301       }
00302     case MARSHAL_HTTP_HEADER:
00303     case MARSHAL_HTTP_HEADER_REQ:
00304     case MARSHAL_HTTP_HEADER_RESP:
00305       {
00306         HTTPHdr h(bufp, offset);
00307         print_http_hdr_impl(h);
00308         printf("\n");
00309         break;
00310       }
00311 
00312 
00313     default:
00314       printf("\n");
00315     }
00316 
00317     offset += mo->m_length;
00318   } while (offset < max_offset);
00319 }
00320 
00321 void
00322 walk_mstring(MBuffer * bufp, int32_t offset)
00323 {
00324   int bufindex = 0;
00325   int dumpoffset = 0;
00326   char fbuf[4096];
00327 
00328 //    int32_t soffset = field_offset;
00329 //    soffset <<= MARSHAL_ALIGNMENT;
00330 //    printf("offset: %d.  shifted field_offset: %d\n",
00331 //         field_offset, soffset);
00332 
00333   memset(fbuf, 0, 4096);
00334   mstring_print(bufp, offset, fbuf, 4095, &bufindex, &dumpoffset);
00335 
00336   printf("%s", fbuf);
00337 }
00338 
00339 void
00340 walk_mime_field(MIMEField f)
00341 {
00342 
00343   int bufindex = 0;
00344   int dumpoffset = 0;
00345   char fbuf[4096];
00346 
00347 //    int32_t soffset = field_offset;
00348 //    soffset <<= MARSHAL_ALIGNMENT;
00349 //    printf("offset: %d.  shifted field_offset: %d\n",
00350 //         field_offset, soffset);
00351 
00352   MIMEFieldImpl *fi = MIMEFieldPtr(f.m_buffer, f.m_offset);
00353   memset(fbuf, 0, 4096);
00354   mime_field_print(f.m_buffer, f.m_offset, fbuf, 4095, &bufindex, &dumpoffset);
00355 
00356   printf("(%d,%d) [%d,%d,%d] %s", (int) fi->m_nvalues, (int) fi->m_flags,
00357          (int) fi->m_name_offset, (int) fi->m_values_offset, (int) fi->m_next_offset, fbuf);
00358 }
00359 
00360 void
00361 walk_http_resp_hdr(HTTPHdr resp)
00362 {
00363   HTTPHdrImpl *r = HTTPHdrPtr(resp.m_buffer, resp.m_offset);
00364 
00365   printf("Http Response Hdr\n");
00366 
00367   if (r->type != MARSHAL_HTTP_HEADER_RESP) {
00368     printf("Type match failed\n");
00369     return;
00370   }
00371 
00372   int16_t field_offset = r->m_fields_offset;
00373 
00374   while (field_offset != MARSHAL_NULL_OFFSET) {
00375     MIMEFieldImpl *f = MIMEFieldPtr(resp.m_buffer, field_offset);
00376 
00377     MIMEField field(resp.m_buffer, field_offset);
00378     walk_mime_field(field);
00379 
00380     field_offset = f->m_next_offset;
00381   }
00382 }
00383 
00384 void
00385 walk_http_info(HTTPInfo hi)
00386 {
00387   HTTPInfoImpl *info = HTTPInfoPtr(hi.m_buffer, hi.m_offset);
00388 
00389   printf("HttpInfo\n");
00390 
00391   if (info->type != MARSHAL_HTTP_INFO) {
00392     printf("Type match failed\n");
00393     return;
00394   }
00395 
00396   printf("id: %d  rid: %d\n", info->m_id, info->m_rid);
00397   printf("Request Sent Time %s", ctime(&info->m_request_sent_time));
00398   printf("Response Received Time %s\n", ctime(&info->m_response_received_time));
00399   printf("Request Offset: %d   Response Offset: %d", info->m_request_offset, info->m_response_offset);
00400 }
00401 
00402 void
00403 print_http_info_impl(HTTPInfo hi)
00404 {
00405   HTTPInfoImpl *info = HTTPInfoPtr(hi.m_buffer, hi.m_offset);
00406 
00407   if (info->type != MARSHAL_HTTP_INFO) {
00408     printf("Type match failed\n");
00409     return;
00410   }
00411 
00412   printf("id: %d  rid: %d  req: %d  resp: %d",
00413          info->m_id, info->m_rid, info->m_request_offset, info->m_response_offset);
00414 }
00415 
00416 void
00417 print_http_hdr_impl(HTTPHdr h)
00418 {
00419   HTTPHdrImpl *hdr = HTTPHdrPtr(h.m_buffer, h.m_offset);
00420 
00421   if (hdr->type == MARSHAL_HTTP_HEADER) {
00422     printf("fields: %d", (int) hdr->m_fields_offset);
00423     return;
00424   } else if (hdr->type == MARSHAL_HTTP_HEADER_REQ) {
00425     printf("method: %d  url: %d  fields: %d",
00426            (int) hdr->u.req.m_method_offset, (int) hdr->u.req.m_url_offset, (int) hdr->m_fields_offset);
00427   } else if (hdr->type == MARSHAL_HTTP_HEADER_RESP) {
00428     printf("status: %d  reason: %d  fields: %d",
00429            (int) hdr->u.resp.m_status, (int) hdr->u.resp.m_reason_offset, (int) hdr->m_fields_offset);
00430   } else {
00431     printf("Type match failed\n");
00432     return;
00433   }
00434 
00435 
00436 }

Generated by  doxygen 1.7.1