00001
00010 #include "protocol.h"
00011
00012
00014
00015
00016
00036 tList* get_protocol_header_list(Buffer buf, char deli, int fstline, int rcntnt)
00037 {
00038 tList* lp;
00039
00040 if (buf.buf==NULL) return NULL;
00041
00042 lp = get_protocol_header_list_seq(NULL, buf, deli, fstline, rcntnt);
00043 if (lp!=NULL) lp = find_tList_top(lp);
00044
00045 return lp;
00046 }
00047
00048
00049
00058 Buffer restore_protocol_header(tList* list, char* deli, int mode, int* hdsz)
00059 {
00060 Buffer buf;
00061
00062 buf = init_Buffer();
00063 if (list==NULL) return buf;
00064
00065 buf = make_Buffer(RECVBUFSZ);
00066
00067 while(list!=NULL) {
00068 if (!strcmp((const char*)(list->ldat.key.buf), HDLIST_FIRST_LINE_KEY)) {
00069 copy_Buffer(&(list->ldat.val), &buf);
00070 cat_s2Buffer("\r\n", &buf);
00071 }
00072 else if (!strcmp((const char*)(list->ldat.key.buf), HDLIST_CONTINUE)) {
00073 buf.buf[buf.vldsz] = CHAR_TAB;
00074 buf.vldsz++;
00075 cat_Buffer(&(list->ldat.val), &buf);
00076 cat_s2Buffer("\r\n", &buf);
00077 }
00078 else if (!strcmp((const char*)(list->ldat.key.buf), HDLIST_END_KEY)) {
00079 break;
00080 }
00081 else if (strcmp((const char*)(list->ldat.key.buf), HDLIST_CONTENTS_KEY)) {
00082 cat_Buffer(&(list->ldat.key), &buf);
00083 cat_s2Buffer(deli, &buf);
00084 cat_Buffer(&(list->ldat.val), &buf);
00085 cat_s2Buffer("\r\n", &buf);
00086 }
00087
00088 list = list->next;
00089 }
00090 if (buf.vldsz>0) cat_s2Buffer("\r\n", &buf);
00091
00092 if (hdsz!=NULL) *hdsz = buf.vldsz;
00093
00094 if (mode==ON) {
00095 int nn = 1;
00096 tList* pl = strncmp_tList(list, (char*)HDLIST_CONTENTS_KEY, 0, nn);
00097 while (pl!=NULL && pl->ldat.val.buf!=NULL) {
00098 cat_Buffer(&(pl->ldat.val), &buf);
00099 pl = strncmp_tList(list, (char*)HDLIST_CONTENTS_KEY, 0, ++nn);
00100 }
00101 }
00102
00103 return buf;
00104 }
00105
00106
00107
00113 Buffer restore_protocol_contents(tList* list)
00114 {
00115 Buffer buf;
00116 tList* lp;
00117 int nn = 1;
00118
00119 buf = make_Buffer(BUFSZ);
00120
00121 lp = strncmp_tList(list, (char*)HDLIST_CONTENTS_KEY, 0, nn);
00122 while (lp!=NULL && lp->ldat.val.buf!=NULL) {
00123 cat_Buffer(&(lp->ldat.val), &buf);
00124 lp = strncmp_tList(list, (char*)HDLIST_CONTENTS_KEY, 0, ++nn);
00125 }
00126
00127 return buf;
00128 }
00129
00130
00131
00137 void set_protocol_contents(tList* list, Buffer contents)
00138 {
00139 while (list!=NULL) {
00140 if (list->ldat.key.buf!=NULL && !strcmp((char*)list->ldat.key.buf, HDLIST_CONTENTS_KEY)) {
00141 free_Buffer(&list->ldat.val);
00142 list->ldat.val = dup_Buffer(contents);
00143
00144 while (is_header_continue(list)) del_tList_node(&(list->next));
00145 break;
00146 }
00147
00148 list = list->next;
00149 }
00150
00151 return;
00152 }
00153
00154
00155
00187 tList* get_protocol_header_list_seq(tList* lp, Buffer buf, char deli, int fstline, int rcntnt)
00188 {
00189 static int crlfCount = 0;
00190 static int inContents = FALSE;
00191 int i=0, n=0, size;
00192 Buffer key, data;
00193
00194 if (buf.buf==NULL) return NULL;
00195
00196 size = buf.vldsz;
00197 data = make_Buffer(size+1);
00198 key = make_Buffer(LBUF);
00199
00200 if (lp==NULL) {
00201 crlfCount = 0;
00202 inContents = FALSE;
00203 }
00204
00205
00206 if (fstline && lp==NULL) {
00207 while(buf.buf[i]!=0x0a && buf.buf[i]!='\0' && i<size && n<size) {
00208 data.buf[n++] = buf.buf[i++];
00209 }
00210 if (data.buf[n-1]==0x0d) {
00211 i--;
00212 n--;
00213 }
00214
00215 data.buf[n] = '\0';
00216 data.vldsz = n;
00217 copy_s2Buffer(HDLIST_FIRST_LINE_KEY, &key);
00218 lp = add_tList_node_Buffer(NULL, key, data);
00219
00220 if (buf.buf[i]=='\0' || i==size) {
00221 free_Buffer(&key);
00222 free_Buffer(&data);
00223 return lp;
00224 }
00225 clear_Buffer(&key);
00226 clear_Buffer(&data);
00227 }
00228
00229
00230 while(buf.buf[i]!='\0' && i<size && !inContents) {
00231
00232 if (i+1<size && buf.buf[i]==0x0d && buf.buf[i+1]==0x0a) {
00233 i = i + 2;
00234 if (crlfCount==1) {
00235 copy_s2Buffer(HDLIST_END_KEY, &key);
00236 lp = add_tList_node_Buffer(lp, key, data);
00237 crlfCount = 0;
00238 inContents = TRUE;
00239 break;
00240 }
00241 else crlfCount = 1;
00242 }
00243 else if (buf.buf[i]==0x0a) {
00244 i = i + 1;
00245 if (crlfCount==1) {
00246 copy_s2Buffer(HDLIST_END_KEY, &key);
00247 lp = add_tList_node_Buffer(lp, key, data);
00248 crlfCount = 0;
00249 inContents = TRUE;
00250 break;
00251 }
00252 else crlfCount = 1;
00253 }
00254
00255
00256 if (i+1<size && buf.buf[i]==0x0d && buf.buf[i+1]==0x0a) {
00257 i = i + 2;
00258 if (crlfCount==1) {
00259 copy_s2Buffer(HDLIST_END_KEY, &key);
00260 lp = add_tList_node_Buffer(lp, key, data);
00261 crlfCount = 0;
00262 inContents = TRUE;
00263 break;
00264 }
00265 }
00266 else if (i<size && buf.buf[i]==0x0a) {
00267 i = i + 1;
00268 if (crlfCount==1) {
00269 copy_s2Buffer(HDLIST_END_KEY, &key);
00270 lp = add_tList_node_Buffer(lp, key, data);
00271 crlfCount = 0;
00272 inContents = TRUE;
00273 break;
00274 }
00275 }
00276
00277 if (buf.buf[i]=='\0' || i==size) break;
00278
00279
00280 n = 0;
00281 if (buf.buf[i]==CHAR_TAB || buf.buf[i]==' ') {
00282 copy_s2Buffer(HDLIST_CONTINUE, &key);
00283 }
00284 else {
00285 while(buf.buf[i]!=deli && buf.buf[i]!='\0' && i<size && n<LBUF) {
00286 key.buf[n++] = buf.buf[i++];
00287 }
00288 key.buf[n] = '\0';
00289 key.vldsz = n;
00290 }
00291 while ((buf.buf[i]==deli ||buf.buf[i]==' '||buf.buf[i]==CHAR_TAB) && i<size) i++;
00292
00293
00294 n = 0;
00295 while(buf.buf[i]!=0x0a && buf.buf[i]!='\0' && i<size && n<size) {
00296 data.buf[n++] = buf.buf[i++];
00297 }
00298 if (data.buf[n-1]==0x0d) {
00299 i--;
00300 n--;
00301 }
00302 data.buf[n] = '\0';
00303 data.vldsz = n;
00304
00305 lp = add_tList_node_Buffer(lp, key, data);
00306
00307 crlfCount = 0;
00308 clear_Buffer(&key);
00309 clear_Buffer(&data);
00310 }
00311
00312
00313 if (rcntnt && i<size && inContents) {
00314 crlfCount = 0;
00315 clear_Buffer(&key);
00316 clear_Buffer(&data);
00317 copy_s2Buffer(HDLIST_CONTENTS_KEY, &key);
00318
00319 n = 0;
00320 while(i<size && n<size) {
00321 data.buf[n++] = buf.buf[i++];
00322 }
00323 data.vldsz = n;
00324
00325 lp = add_tList_node_Buffer(lp, key, data);
00326 }
00327
00328 free_Buffer(&key);
00329 free_Buffer(&data);
00330
00331 return lp;
00332 }
00333
00334
00335
00348 tList* get_protocol_header_list_file(char* fname, char deli, int fstline, int rcntnt)
00349 {
00350 Buffer buf;
00351 tList* lp;
00352
00353 if (fname==NULL) return NULL;
00354
00355 buf = read_Buffer_file(fname);
00356 lp = get_protocol_header_list(buf, deli, fstline, rcntnt);
00357
00358 return lp;
00359 }
00360
00361
00362
00363
00365
00366
00380 Buffer search_protocol_header(tList* list, char* key, int no)
00381 {
00382 tList* pp;
00383 Buffer buf;
00384
00385 buf = init_Buffer();
00386 if (list==NULL || key==NULL) return buf;
00387
00388 pp = strncasecmp_tList(list, key, 0, no);
00389 if (pp!=NULL) {
00390 buf = dup_Buffer(pp->ldat.val);
00391
00392 while (is_header_continue(pp)) {
00393 cat_s2Buffer("\r\n", &buf);
00394 pp = pp->next;
00395 cat_Buffer(&(pp->ldat.val), &buf);
00396 }
00397 }
00398
00399 return buf;
00400 }
00401
00402
00403
00417 Buffer search_protocol_header_item(tList* list, char* key, int no, char deli, int nm)
00418 {
00419 Buffer buf, itm;
00420
00421 buf = search_protocol_header(list, key, no);
00422 if (buf.buf==NULL) return buf;
00423
00424 itm = cawk_Buffer(buf, deli, nm);
00425 free_Buffer(&buf);
00426
00427 return itm;
00428 }
00429
00430
00431
00446 Buffer search_protocol_header_value(tList* list, char* key, char* data, int no)
00447 {
00448 Buffer buf;
00449 char* str;
00450 int len, nm;
00451
00452 buf = init_Buffer();
00453 if (list==NULL || key==NULL) return buf;
00454
00455 if (data==NULL) {
00456 buf = search_protocol_header(list, key, no);
00457 return buf;
00458 }
00459
00460 buf = init_Buffer();
00461 len = (int)strlen(data);
00462
00463 nm = 0;
00464 while (list!=NULL) {
00465 if (list->ldat.key.buf!=NULL && !strcasecmp((char*)list->ldat.key.buf, key)) {
00466 str = (char*)list->ldat.val.buf;
00467
00468 if (str!=NULL && !strncasecmp(str, data, len)) {
00469 nm++;
00470 if (no==nm) {
00471 buf = make_Buffer_bystr(str);
00472 return buf;
00473 }
00474 }
00475
00476 while (is_header_continue(list)) {
00477 list = list->next;
00478 str = (char*)list->ldat.val.buf;
00479 if (str!=NULL && !strncasecmp(str, data, len)) {
00480 nm++;
00481 if (no==nm) {
00482 buf = make_Buffer_bystr(str);
00483 return buf;
00484 }
00485 }
00486 }
00487 }
00488
00489 list = list->next;
00490 }
00491
00492 return buf;
00493 }
00494
00495
00496
00511 Buffer search_protocol_header_partvalue(tList* list, char* key, char* data, int no)
00512 {
00513 Buffer buf;
00514 char* str;
00515 int nm;
00516
00517 buf = init_Buffer();
00518 if (list==NULL || key==NULL) return buf;
00519
00520 if (data==NULL) {
00521 buf = search_protocol_header(list, key, no);
00522 return buf;
00523 }
00524
00525 buf = init_Buffer();
00526
00527
00528 nm = 0;
00529 while (list!=NULL) {
00530 if (list->ldat.key.buf!=NULL && !strcasecmp((char*)list->ldat.key.buf, key)) {
00531 str = (char*)list->ldat.val.buf;
00532
00533 if (str!=NULL && strstrcase(str, data)) {
00534 nm++;
00535 if (no==nm) {
00536 buf = make_Buffer_bystr(str);
00537 return buf;
00538 }
00539 }
00540
00541 while (is_header_continue(list)) {
00542 list = list->next;
00543 str = (char*)list->ldat.val.buf;
00544 if (str!=NULL && strstrcase(str, data)) {
00545 nm++;
00546 if (no==nm) {
00547 buf = make_Buffer_bystr(str);
00548 return buf;
00549 }
00550 }
00551 }
00552 }
00553
00554 list = list->next;
00555 }
00556
00557 return buf;
00558 }
00559
00560
00561
00562
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586 int set_protocol_header(tList* list, char* key, char* value, int no, int add_mode)
00587 {
00588 int cn = set_value_tList(list, key, no, value, OFF);
00589
00590
00591 if (add_mode==ON && cn==0) {
00592 tList* pm = strncmp_tList(list, (char*)HDLIST_END_KEY, 0, 1);
00593 if (pm!=NULL && pm->prev!=NULL) {
00594 add_tList_node_str(pm->prev, key, value);
00595 }
00596 else {
00597 add_tList_node_str(list, key, value);
00598 }
00599 }
00600
00601 return cn;
00602 }
00603
00604
00605
00613 int search_crlfcrlf(char* mesg)
00614 {
00615 int cr = 0;
00616 int lf = 0;
00617 int i;
00618
00619 if (mesg==NULL) return JBXL_ARGS_ERROR;
00620 if (mesg[0]==0x0a) return 2;
00621 if (mesg[0]==0x0d && mesg[1]==0x0a) return 2;
00622
00623 i = 0;
00624 while(mesg[i]!='\0') {
00625 if (mesg[i]==0x0d) cr++;
00626 else if (mesg[i]==0x0a) lf++;
00627 else {
00628 cr = lf = 0;
00629 }
00630
00631 if (lf==2) return i+1;
00632 i++;
00633 }
00634 return 0;
00635 }
00636
00637
00638
00645 int is_header_continue(tList* pp)
00646 {
00647 if (pp==NULL || pp->next==NULL || pp->next->ldat.key.buf==NULL) return FALSE;
00648 if (!strcmp((const char*)pp->next->ldat.key.buf, HDLIST_CONTINUE)) return TRUE;
00649 return FALSE;
00650 }
00651
00652
00653
00659 void print_protocol_header(tList* pp, int content)
00660 {
00661 if (pp==NULL) return;
00662
00663 if (pp->ldat.id==TLIST_ANCHOR_NODE) pp = pp->next;
00664 while (pp!=NULL) {
00665
00666 if (content==ON || !ex_strcmp(HDLIST_CONTENTS_KEY, (char*)pp->ldat.key.buf)) {
00667 print_message("[%s] [%s]\n", (char*)pp->ldat.key.buf, (char*)pp->ldat.val.buf);
00668 }
00669 pp = pp->next;
00670 }
00671 return;
00672 }
00673
00674
00675
00681 tList* find_protocol_end(tList* lp)
00682 {
00683 tList* end = search_key_tList(lp, HDLIST_END_KEY, 1);
00684 if (end==NULL) end = search_key_tList(lp, HDLIST_CONTENTS_KEY, 1);
00685
00686 if (end!=NULL) end = end->prev;
00687 else end = find_tList_end(lp);
00688
00689 return end;
00690 }
00691