add port number handling to bbsmnparser_item_t.
@@ -1,7 +1,7 @@ | ||
1 | 1 | /* |
2 | 2 | * test_bbsmenuparser.c |
3 | 3 | * |
4 | - * Copyright (c) 2009-2012 project bchan | |
4 | + * Copyright (c) 2009-2015 project bchan | |
5 | 5 | * |
6 | 6 | * This software is provided 'as-is', without any express or implied |
7 | 7 | * warranty. In no event will the authors be held liable for any damages |
@@ -395,6 +395,96 @@ | ||
395 | 395 | return UNITTEST_RESULT_FAIL; |
396 | 396 | } |
397 | 397 | |
398 | +typedef struct { | |
399 | + UB *url; | |
400 | + W url_len; | |
401 | + UB *expected_host; | |
402 | + W expected_host_len; | |
403 | + UH expected_port; | |
404 | + UB *expected_board; | |
405 | + W expected_board_len; | |
406 | +} test_bbsmnparser_item_url_t; | |
407 | + | |
408 | +LOCAL UNITTEST_RESULT test_bbsmnparser_item_url_common(test_bbsmnparser_item_url_t *data) | |
409 | +{ | |
410 | + bbsmncache_t *cache; | |
411 | + bbsmnparser_t *parser; | |
412 | + bbsmnparser_item_t *item; | |
413 | + UNITTEST_RESULT ret = UNITTEST_RESULT_PASS; | |
414 | + UB *host, *board; | |
415 | + W host_len, board_len; | |
416 | + UH port; | |
417 | + | |
418 | + cache = bbsmncache_new(); | |
419 | + if (cache == NULL) { | |
420 | + return UNITTEST_RESULT_FAIL; | |
421 | + } | |
422 | + parser = bbsmnparser_new(cache); | |
423 | + if (parser == NULL) { | |
424 | + bbsmncache_delete(cache); | |
425 | + return UNITTEST_RESULT_FAIL; | |
426 | + } | |
427 | + | |
428 | + item = bbsmnparser_newboarditem(parser, NULL, 0, data->url, data->url_len); | |
429 | + if (item == NULL) { | |
430 | + ret = UNITTEST_RESULT_FAIL; | |
431 | + } | |
432 | + bbsmnparser_item_gethostboard(item, &host, &host_len, &port, &board, &board_len); | |
433 | + if (host_len != data->expected_host_len) { | |
434 | + printf("host length fail: expected = %d, result = %d\n", data->expected_host_len, host_len); | |
435 | + ret = UNITTEST_RESULT_FAIL; | |
436 | + } else if (strncmp(host, data->expected_host, host_len) != 0) { | |
437 | + printf("host fail\n"); | |
438 | + ret = UNITTEST_RESULT_FAIL; | |
439 | + } | |
440 | + if (port != data->expected_port) { | |
441 | + printf("port fail: expected = %d, result = %d\n", data->expected_port, port); | |
442 | + ret = UNITTEST_RESULT_FAIL; | |
443 | + } | |
444 | + if (board_len != data->expected_board_len) { | |
445 | + printf("board length fail: expected = %d, result = %d\n", data->expected_board_len, board_len); | |
446 | + ret = UNITTEST_RESULT_FAIL; | |
447 | + } else if (strncmp(board, data->expected_board, board_len) != 0) { | |
448 | + printf("board fail\n"); | |
449 | + ret = UNITTEST_RESULT_FAIL; | |
450 | + } | |
451 | + | |
452 | + bbsmnparser_delete(parser); | |
453 | + bbsmncache_delete(cache); | |
454 | + | |
455 | + return ret; | |
456 | +} | |
457 | + | |
458 | +LOCAL UNITTEST_RESULT test_bbsmnparser_item_url_1(test_bbsmnparser_item_url_t *data) | |
459 | +{ | |
460 | + UB url[] = "http://aaa.bbb.ccc/test/"; | |
461 | + W url_len = strlen(url); | |
462 | + UB host[] = "aaa.bbb.ccc"; | |
463 | + W host_len = strlen(host); | |
464 | + UH port = 80; | |
465 | + UB board[] = "test"; | |
466 | + W board_len = strlen(board); | |
467 | + test_bbsmnparser_item_url_t testdata = { | |
468 | + url, url_len, host, host_len, port, board, board_len | |
469 | + }; | |
470 | + return test_bbsmnparser_item_url_common(&testdata); | |
471 | +} | |
472 | + | |
473 | +LOCAL UNITTEST_RESULT test_bbsmnparser_item_url_2(test_bbsmnparser_item_url_t *data) | |
474 | +{ | |
475 | + UB url[] = "http://aaa.bbb.ccc:65000/test/"; | |
476 | + W url_len = strlen(url); | |
477 | + UB host[] = "aaa.bbb.ccc"; | |
478 | + W host_len = strlen(host); | |
479 | + UH port = 65000; | |
480 | + UB board[] = "test"; | |
481 | + W board_len = strlen(board); | |
482 | + test_bbsmnparser_item_url_t testdata = { | |
483 | + url, url_len, host, host_len, port, board, board_len | |
484 | + }; | |
485 | + return test_bbsmnparser_item_url_common(&testdata); | |
486 | +} | |
487 | + | |
398 | 488 | EXPORT VOID test_bbsmnparser_main(unittest_driver_t *driver) |
399 | 489 | { |
400 | 490 | UNITTEST_DRIVER_REGIST(driver, test_bbsmnparser_1); |
@@ -412,4 +502,6 @@ | ||
412 | 502 | UNITTEST_DRIVER_REGIST(driver, test_bbsmnparser_13); |
413 | 503 | UNITTEST_DRIVER_REGIST(driver, test_bbsmnparser_14); |
414 | 504 | UNITTEST_DRIVER_REGIST(driver, test_bbsmnparser_15); |
505 | + UNITTEST_DRIVER_REGIST(driver, test_bbsmnparser_item_url_1); | |
506 | + UNITTEST_DRIVER_REGIST(driver, test_bbsmnparser_item_url_2); | |
415 | 507 | } |
@@ -1,7 +1,7 @@ | ||
1 | 1 | /* |
2 | 2 | * bbsmenuparser.c |
3 | 3 | * |
4 | - * Copyright (c) 2009-2012 project bchan | |
4 | + * Copyright (c) 2009-2015 project bchan | |
5 | 5 | * |
6 | 6 | * This software is provided 'as-is', without any express or implied |
7 | 7 | * warranty. In no event will the authors be held liable for any damages |
@@ -262,11 +262,12 @@ | ||
262 | 262 | return 0; /* TODO */ |
263 | 263 | } |
264 | 264 | |
265 | -EXPORT VOID bbsmnparser_item_gethostboard(bbsmnparser_item_t *item, UB **host, W *host_len, UB **board, W *board_len) | |
265 | +EXPORT VOID bbsmnparser_item_gethostboard(bbsmnparser_item_t *item, UB **host, W *host_len, UH *port, UB **board, W *board_len) | |
266 | 266 | { |
267 | 267 | W i = 0; |
268 | - UB *host0 = NULL, *board0 = NULL; | |
269 | - W host_len0 = 0, board_len0 = 0; | |
268 | + UB *host0 = NULL, *port0 = NULL, *board0 = NULL; | |
269 | + W host_len0 = 0, port_len0 = 0, board_len0 = 0; | |
270 | + Bool port_exist = False; | |
270 | 271 | |
271 | 272 | host0 = item->url + 7; |
272 | 273 | for (i=7; i < item->url_len; i++) { |
@@ -273,10 +274,30 @@ | ||
273 | 274 | if (item->url[i] == '/') { |
274 | 275 | break; |
275 | 276 | } |
277 | + if (item->url[i] == ':') { | |
278 | + port_exist = True; | |
279 | + break; | |
280 | + } | |
276 | 281 | host_len0++; |
277 | 282 | } |
283 | + i++; | |
278 | 284 | |
279 | - i++; | |
285 | + port0 = item->url + i; | |
286 | + if (port_exist != False) { | |
287 | + for (; i < item->url_len; i++) { | |
288 | + if (item->url[i] == '/') { | |
289 | + break; | |
290 | + } | |
291 | + port_len0++; | |
292 | + } | |
293 | + i++; | |
294 | + } | |
295 | + if (port_len0 > 0) { | |
296 | + *port = atoi(port0); | |
297 | + } else { | |
298 | + *port = 80; | |
299 | + } | |
300 | + | |
280 | 301 | board0 = item->url + i; |
281 | 302 | for (; i < item->url_len; i++) { |
282 | 303 | if (item->url[i] == '/') { |
@@ -1,7 +1,7 @@ | ||
1 | 1 | /* |
2 | 2 | * bbsmenuparser.h |
3 | 3 | * |
4 | - * Copyright (c) 2009-2012 project bchan | |
4 | + * Copyright (c) 2009-2015 project bchan | |
5 | 5 | * |
6 | 6 | * This software is provided 'as-is', without any express or implied |
7 | 7 | * warranty. In no event will the authors be held liable for any damages |
@@ -43,7 +43,7 @@ | ||
43 | 43 | |
44 | 44 | IMPORT VOID bbsmnparser_item_delete(bbsmnparser_item_t *item); |
45 | 45 | IMPORT Bool bbsmnparser_item_checkboradurl(bbsmnparser_item_t *item); |
46 | -IMPORT VOID bbsmnparser_item_gethostboard(bbsmnparser_item_t *item, UB **host, W *host_len, UB **board, W *board_len); | |
46 | +IMPORT VOID bbsmnparser_item_gethostboard(bbsmnparser_item_t *item, UB **host, W *host_len, UH *port, UB **board, W *board_len); | |
47 | 47 | |
48 | 48 | IMPORT bbsmnparser_t* bbsmnparser_new(bbsmncache_t *cache); |
49 | 49 | IMPORT VOID bbsmnparser_delete(bbsmnparser_t *parser); |
@@ -1,7 +1,7 @@ | ||
1 | 1 | /* |
2 | 2 | * main.c |
3 | 3 | * |
4 | - * Copyright (c) 2009-2014 project bchan | |
4 | + * Copyright (c) 2009-2015 project bchan | |
5 | 5 | * |
6 | 6 | * This software is provided 'as-is', without any express or implied |
7 | 7 | * warranty. In no event will the authors be held liable for any damages |
@@ -614,6 +614,7 @@ | ||
614 | 614 | W fnd; |
615 | 615 | UB *host, *board; |
616 | 616 | W host_len, board_len; |
617 | + UH port; | |
617 | 618 | TC *title; |
618 | 619 | W title_len; |
619 | 620 |
@@ -625,7 +626,7 @@ | ||
625 | 626 | if (item->category != NULL) { |
626 | 627 | return; |
627 | 628 | } |
628 | - bbsmnparser_item_gethostboard(item, &host, &host_len, &board, &board_len); | |
629 | + bbsmnparser_item_gethostboard(item, &host, &host_len, &port, &board, &board_len); | |
629 | 630 | subject = bchanl_subjecthash_search(bchanl->subjecthash, host, host_len, board, board_len); |
630 | 631 | if (subject == NULL) { |
631 | 632 | DP(("not found by subject hash")); |
@@ -734,8 +735,9 @@ | ||
734 | 735 | W err; |
735 | 736 | UB *host, *board; |
736 | 737 | W host_len, board_len; |
738 | + UH port; | |
737 | 739 | |
738 | - bbsmnparser_item_gethostboard(item, &host, &host_len, &board, &board_len); | |
740 | + bbsmnparser_item_gethostboard(item, &host, &host_len, &port, &board, &board_len); | |
739 | 741 | err = bchanl_subjecthash_append(bchanl->subjecthash, host, host_len, board, board_len, item->title, item->title_len); |
740 | 742 | return err; |
741 | 743 | } |