myht_util.c
最終更新:2009/12/19
001: /* myht_util.c
002: * HTTP 用ユーティリティ
003: */
004:
005: #include "myht_util.h"
006:
007: /*
008: * 1行の文字列からデリミタで区切られた3つの文字列を読み出す関数
009: * 文字列の領域も確保する。
010: * 各文字列はヌル文字で終端される。
011: * 返り値:
012: * 読み出せた文字列の個数。
013: * エラー時には -1。
014: * ・入力文字列は書き換えられることに注意
015: */
016:
017: int read_3strs (
018: char *line /* IN: 入力文字列 */
019: , char **x1 /* OUT: 1番目の文字列へのポインタ */
020: , char **x2 /* OUT: 2番目の文字列へのポインタ */
021: , char **x3 /* OUT: 3番目の文字列へのポインタ */
022: , char delim /* IN: デリミタ文字 */
023: ) {
024: char *pos = NULL;
025:
026: if (!line)
027: return -1;
028:
029: pos = *x1 = line;
030:
031: while (*pos && *pos != delim)
032: pos++;
033:
034: if (! *pos)
035: return 1;
036:
037: *pos = '\0';
038:
039: *x2 = ++pos;
040: while (*pos && *pos != delim)
041: pos++;
042:
043: if (! *pos)
044: return 2;
045:
046: *pos = '\0';
047:
048: *x3 = ++pos;
049: /* もし、末尾に空白があってもそのまま。 */
050:
051: return 3;
052: }
053:
054: /* ヘッダ文字列からヘッダ名・ヘッダの値を抽出し、
055: * テーブルに保存する関数
056: *
057: */
058:
059: void set_entity_header_to_tab (
060: char *line
061: , apr_table_t *tab
062: ) {
063: char *name = NULL;
064: char *value = NULL;
065: char *pos = NULL;
066:
067: if (!line || !tab)
068: return;
069:
070: name = line;
071:
072: pos = strchr(line, ':');
073:
074: if (!pos)
075: return; /* ':' が見つからなかった時は何もしない。 */
076:
077: *pos = '\0'; /* name の終端 */
078:
079: /* ':' の前の空白の処理をしていないが、本来はすべきだろう。 */
080:
081: /* ':' の後の空白のスキップ */
082:
083: pos++;
084: while(*pos && (*pos == ' ' || *pos == '\t'))
085: pos++;
086:
087: value = pos;
088:
089: if (name && value)
090: apr_table_set(tab, name, value);
091: }
092:
093: /*
094: * テーブル各キー・値の表示
095: */
096:
097: int disp_tab (void *ctx, const char *key, const char *value) {
098: apr_file_t *out = (apr_file_t*)ctx;
099: apr_file_printf(out, "[%s]=[%s]\n", key, value);
100: return TRUE;/* TRUE:continue iteration. FALSE:stop iteration */
101: }
102:
103: /* URI のパース
104: * 成功:1
105: * 失敗:0
106: */
107:
108: int my_uri_parse(
109: apr_pool_t *pool
110: , const char *uri_str
111: , apr_uri_t *uri
112: ) {
113: apr_status_t rv = APR_SUCCESS;
114: rv = apr_uri_parse(pool, uri_str, uri);
115: if (rv != APR_SUCCESS) {
116: return 0;
117: }
118:
119: /* URI スキームは "http" か "https" 限定 */
120: if ((!uri->scheme) ||
121: (uri->scheme && (strcmp(uri->scheme, "http") && strcmp(uri->scheme, "https")))
122: ) {
123: return 0;
124: }
125:
126: /* ポート番号の補完 */
127: if (!uri->port_str) {
128: /* URI にポート番号がない場合には、uri.port_str に値はセットされない。 */
129: /* ここでは、スキーマ文字列からデフォルトポートを推測している。 */
130:
131: uri->port = apr_uri_port_of_scheme(uri->scheme);
132: uri->port_str = apr_itoa(pool, uri->port);
133: }
134:
135: /* パスの補完 */
136: if (!uri->path) {
137: /* URI にパスが指定されていない場合は "/" で補完する。 */
138: uri->path = "/";
139: }
140:
141: /* 最終的にホストネームとポート番号、パスを取得できたか確認 */
142: if (!uri->hostname || uri->port <= 0 || !uri->path) {
143: return 0;
144: }
145:
146: return 1;
147: }
![]() | KAKU PROJECT (2009) |