Ignore:
Timestamp:
Nov 23, 2005 8:56:30 PM (18 years ago)
Author:
bennylp
Message:

Added tel: uri and user-defined uri parser

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjsip/src/pjsip/sip_parser.c

    r80 r82  
    5050 
    5151#define PJSIP_VERSION           "SIP/2.0" 
    52 #define PJSIP_SYN_ERR_EXCEPTION 1 
    5352 
    5453#define UNREACHED(expr) 
    5554 
    56 //#define IS_NEWLINE(c) ((c)=='\r' || (c)=='\n') 
    57 #define IS_NEWLINE(c)   ((c)=='\r') 
     55#define IS_NEWLINE(c)   ((c)=='\r' || (c)=='\n') 
    5856#define IS_SPACE(c)     ((c)==' ' || (c)=='\t') 
    5957 
     58/* 
     59 * Header parser records. 
     60 */ 
    6061typedef struct handler_rec 
    6162{ 
     
    6667} handler_rec; 
    6768 
    68 static handler_rec handler[127]; 
     69static handler_rec handler[PJSIP_MAX_HEADER_TYPES]; 
    6970static unsigned handler_count; 
    7071static int parser_is_initialized; 
     72 
     73/* 
     74 * URI parser records. 
     75 */ 
     76typedef struct uri_parser_rec 
     77{ 
     78    pj_str_t                 scheme; 
     79    pjsip_parse_uri_func    *parse; 
     80} uri_parser_rec; 
     81 
     82static uri_parser_rec uri_handler[PJSIP_MAX_URI_TYPES]; 
     83static unsigned uri_handler_count; 
    7184 
    7285/* 
    7386 * Global vars (also extern). 
    7487 */ 
     88int PJSIP_SYN_ERR_EXCEPTION; 
     89 
    7590const pj_str_t  pjsip_USER_STR      = { "user", 4}; 
    7691const pj_str_t  pjsip_METHOD_STR    = { "method", 6}; 
     
    247262    initialized = 1; 
    248263 
     264    /* 
     265     * Syntax error exception number. 
     266     */ 
     267    status = pj_exception_id_alloc("PJSIP: syntax error",  
     268                                   &PJSIP_SYN_ERR_EXCEPTION); 
     269    PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); 
     270 
     271    /* 
     272     * Init character input spec (cis) 
     273     */ 
     274 
    249275    pj_cis_buf_init(&cis_buf); 
    250276 
     
    310336    pj_cis_invert(&pjsip_DISPLAY_SPEC); 
    311337 
     338    /* 
     339     * Register URI parsers. 
     340     */ 
     341 
     342    status = pjsip_register_uri_parser("sip", &int_parse_sip_url); 
     343    PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); 
     344 
     345    status = pjsip_register_uri_parser("sips", &int_parse_sip_url); 
     346    PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); 
     347 
     348    /* 
     349     * Register header parsers. 
     350     */ 
     351 
    312352    status = pjsip_register_hdr_parser( "Accept", NULL, &parse_hdr_accept); 
    313353    PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); 
     
    374414    PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); 
    375415 
    376     /* Register auth parser. */ 
     416    /*  
     417     * Register auth parser.  
     418     */ 
     419 
    377420    status = pjsip_auth_init_parser(); 
    378421 
     
    383426{ 
    384427    if (!parser_is_initialized) { 
    385         /* Prevent race cond. */ 
    386         pj_enter_critical_section(); 
    387428        if (!parser_is_initialized) { 
    388429            init_parser(); 
    389430            parser_is_initialized = 1; 
    390431        } 
    391         pj_leave_critical_section(); 
    392432    } 
    393433} 
     
    399439 */ 
    400440PJ_INLINE(int) compare_handler( const handler_rec *r1,  
    401                             const char *name,  
    402                             pj_size_t name_len, 
    403                             pj_uint32_t hash ) 
     441                                const char *name,  
     442                                pj_size_t name_len, 
     443                                pj_uint32_t hash ) 
    404444{ 
    405445    PJ_UNUSED_ARG(name_len); 
     
    539579} 
    540580 
     581/* Find URI handler. */ 
     582static pjsip_parse_uri_func* find_uri_handler(const pj_str_t *scheme) 
     583{ 
     584    unsigned i; 
     585    for (i=0; i<uri_handler_count; ++i) { 
     586        if (pj_stricmp_alnum(&uri_handler[i].scheme, scheme)==0) 
     587            return uri_handler[i].parse; 
     588    } 
     589    return NULL; 
     590} 
     591 
     592/* Register URI parser. */ 
     593PJ_DEF(pj_status_t) pjsip_register_uri_parser( char *scheme, 
     594                                               pjsip_parse_uri_func *func) 
     595{ 
     596    if (uri_handler_count >= PJ_ARRAY_SIZE(uri_handler)) 
     597        return PJ_ETOOMANY; 
     598 
     599    uri_handler[uri_handler_count].scheme = pj_str((char*)scheme); 
     600    uri_handler[uri_handler_count].parse = func; 
     601    ++uri_handler_count; 
     602 
     603    return PJ_SUCCESS; 
     604} 
     605 
    541606/* Public function to parse SIP message. */ 
    542607PJ_DEF(pjsip_msg*) pjsip_parse_msg( pj_pool_t *pool,  
     
    548613    pjsip_parse_ctx context; 
    549614 
    550     init_sip_parser(); 
    551  
    552615    pj_scan_init(&scanner, buf, size, PJ_SCAN_AUTOSKIP_WS_HEADER,  
    553616                 &on_syntax_error); 
     
    569632    pj_scanner scanner; 
    570633    pjsip_parse_ctx context; 
    571  
    572     init_sip_parser(); 
    573634 
    574635    pj_scan_init(&scanner, buf, size, PJ_SCAN_AUTOSKIP_WS_HEADER,  
     
    626687            PJ_USE_EXCEPTION; 
    627688 
    628             init_sip_parser(); 
    629  
    630689            pj_scan_init(&scanner, (char*)line, hdr_end-line,  
    631690                         PJ_SCAN_AUTOSKIP_WS_HEADER, &on_syntax_error); 
     
    694753    pjsip_uri *uri = NULL; 
    695754    PJ_USE_EXCEPTION; 
    696  
    697     init_sip_parser(); 
    698755 
    699756    pj_scan_init(&scanner, buf, size, 0, &on_syntax_error); 
     
    872929            } 
    873930 
     931            /* Restore flag. Flag may be set in int_parse_sip_url() */ 
     932            scanner->skip_ws = PJ_SCAN_AUTOSKIP_WS_HEADER; 
     933 
     934            /* Continue parse next header, if any. */ 
    874935            if (!pj_scan_is_eof(scanner) && !IS_NEWLINE(*scanner->curptr)) { 
    875936                goto parse_headers; 
     
    10191080        next_ch = pj_scan_peek( scanner, &pjsip_DISPLAY_SPEC, &scheme); 
    10201081 
    1021         if (next_ch==':' &&  
    1022             (parser_stricmp(scheme, pjsip_SIP_STR)==0 ||  
    1023              parser_stricmp(scheme, pjsip_SIPS_STR)==0))  
    1024         { 
    1025             uri = (pjsip_uri*) 
    1026                 int_parse_sip_url( scanner, pool,  
    1027                                    (opt & PJSIP_PARSE_URI_IN_FROM_TO_HDR)== 0); 
    1028  
    1029         } else if (next_ch==':') { 
    1030  
    1031             /* Not supported. */ 
    1032             PJ_THROW(PJSIP_SYN_ERR_EXCEPTION); 
    1033             UNREACHED({return NULL; /* Not reached. */}); 
     1082        if (next_ch==':') { 
     1083            pjsip_parse_uri_func *func = find_uri_handler(&scheme); 
     1084 
     1085            if (func == NULL) { 
     1086                /* Unsupported URI scheme */ 
     1087                PJ_THROW(PJSIP_SYN_ERR_EXCEPTION); 
     1088            } 
     1089 
     1090            uri = (*func)( scanner, pool,  
     1091                          (opt & PJSIP_PARSE_URI_IN_FROM_TO_HDR)== 0); 
     1092 
    10341093 
    10351094        } else { 
     
    10631122        pj_str_t scheme; 
    10641123        int colon; 
     1124        pjsip_parse_uri_func *func; 
    10651125 
    10661126        /* Get scheme. */ 
     
    10701130        } 
    10711131 
    1072         if ((parser_stricmp(scheme, pjsip_SIP_STR)==0 ||  
    1073             parser_stricmp(scheme, pjsip_SIPS_STR)==0))  
    1074         { 
    1075             return (pjsip_uri*)int_parse_sip_url( scanner, pool, parse_params); 
    1076  
    1077         } else if (parser_stricmp(scheme, pjsip_TEL_STR)==0) { 
    1078             PJ_THROW(PJSIP_SYN_ERR_EXCEPTION); 
    1079             UNREACHED({ return NULL; /* Not reached. */ }) 
     1132        func = find_uri_handler(&scheme); 
     1133        if (func)  { 
     1134            return (pjsip_uri*)(*func)(scanner, pool, parse_params); 
    10801135 
    10811136        } else { 
     1137            /* Unsupported URI scheme */ 
    10821138            PJ_THROW(PJSIP_SYN_ERR_EXCEPTION); 
    10831139            UNREACHED({ return NULL; /* Not reached. */ }) 
     
    17741830    PJ_USE_EXCEPTION; 
    17751831 
    1776     init_sip_parser(); 
    1777  
    17781832    pj_scan_init(&scanner, buf, size, PJ_SCAN_AUTOSKIP_WS_HEADER,  
    17791833                 &on_syntax_error); 
Note: See TracChangeset for help on using the changeset viewer.