kuri.c

最終更新:2009/10/16

kuri.c

001: /* kuri.c
002:  * URI パージングのサンプル
003:  * apr_uri_parse の挙動を確認するプログラム
004:  */
005: 
006: #define USAGE "Usage: kuri <uri>"
007: 
008: #include "apr_general.h"
009: #include "apr_errno.h"
010: #include "apr_strings.h"
011: #include "apr_uri.h"
012: 
013: #include "mystab.h"
014: 
015: int apr_my_main (int ac, char **av, apr_file_t * astdin, apr_file_t * astdout, apr_file_t * astderr, apr_pool_t * pool) {
016: 
017:   int error_flag = 0;
018:   apr_status_t rv = APR_SUCCESS;
019:   
020:   char *uri_str = NULL;
021:   
022:   apr_uri_t uri ;
023:   
024:   if (ac < 2) {
025:     apr_file_printf(astderr, "%s\n", USAGE);
026:     error_flag = 1;
027:     goto _FINALLY_;
028:   }
029: 
030:   uri_str = av[1];
031:   
032:   rv = apr_uri_parse(pool, uri_str, &uri);
033:   if (rv != APR_SUCCESS) {
034:     error_flag = 1;
035:     goto _FINALLY_;
036:   }
037: 
038:   if (!uri.port_str) {
039:     /* URI にポート番号がない場合には、uri.port_str に値はセットされない。 */
040:     /* ここでは、スキーマ文字列からデフォルトポートを推測している。 */
041:     
042:     uri.port = apr_uri_port_of_scheme(uri.scheme);
043:     uri.port_str = apr_itoa(pool, uri.port);
044:     
045:   }
046: 
047:   #define XPRINTS(XSTR,X) apr_file_printf(astdout, "%s=[%s]\n", XSTR, uri.X)
048:   #define XPRINTD(XSTR,X) apr_file_printf(astdout, "%s=[%d]\n", XSTR, uri.X)
049: 
050:   XPRINTS("scheme", scheme);
051:   XPRINTS("hostinfo", hostinfo);
052:   XPRINTS("user", user);
053:   XPRINTS("password", password);
054:   XPRINTS("hostname", hostname);
055:   XPRINTS("port_str", port_str);
056:   XPRINTS("path", path);
057:   XPRINTS("query", query);
058:   XPRINTS("fragment", fragment);
059:   
060:   XPRINTD("port", port);
061: 
062:   _FINALLY_:
063:   
064:   if (error_flag) {
065:     if (rv != APR_SUCCESS) {
066:       char error_buf[256];
067:       apr_file_printf(astderr, "%s\n", apr_strerror(rv, error_buf, sizeof(error_buf)));
068:     }
069:     apr_file_printf(astderr, "failed!\n");
070:     return 1; /* 異常終了 */
071:   }
072:   
073:   apr_file_printf(astdout, "\ndone.\n");
074:   return 0; /* 正常終了 */
075:   
076: } /* end of main */
Copyright (C) KAKU PROJECT (2009)KAKU PROJECT (2009)