/* myht_util.c * HTTP 用ユーティリティ */ #include "myht_util.h" /* * 1行の文字列からデリミタで区切られた3つの文字列を読み出す関数 * 文字列の領域も確保する。 * 各文字列はヌル文字で終端される。 * 返り値: * 読み出せた文字列の個数。 * エラー時には -1。 * ・入力文字列は書き換えられることに注意 */ int read_3strs ( char *line /* IN: 入力文字列 */ , char **x1 /* OUT: 1番目の文字列へのポインタ */ , char **x2 /* OUT: 2番目の文字列へのポインタ */ , char **x3 /* OUT: 3番目の文字列へのポインタ */ , char delim /* IN: デリミタ文字 */ ) { char *pos = NULL; if (!line) return -1; pos = *x1 = line; while (*pos && *pos != delim) pos++; if (! *pos) return 1; *pos = '\0'; *x2 = ++pos; while (*pos && *pos != delim) pos++; if (! *pos) return 2; *pos = '\0'; *x3 = ++pos; /* もし、末尾に空白があってもそのまま。 */ return 3; } /* ヘッダ文字列からヘッダ名・ヘッダの値を抽出し、 * テーブルに保存する関数 * */ void set_entity_header_to_tab ( char *line , apr_table_t *tab ) { char *name = NULL; char *value = NULL; char *pos = NULL; if (!line || !tab) return; name = line; pos = strchr(line, ':'); if (!pos) return; /* ':' が見つからなかった時は何もしない。 */ *pos = '\0'; /* name の終端 */ /* ':' の前の空白の処理をしていないが、本来はすべきだろう。 */ /* ':' の後の空白のスキップ */ pos++; while(*pos && (*pos == ' ' || *pos == '\t')) pos++; value = pos; if (name && value) apr_table_set(tab, name, value); } /* * テーブル各キー・値の表示 */ int disp_tab (void *ctx, const char *key, const char *value) { apr_file_t *out = (apr_file_t*)ctx; apr_file_printf(out, "[%s]=[%s]\n", key, value); return TRUE;/* TRUE:continue iteration. FALSE:stop iteration */ } /* URI のパース * 成功:1 * 失敗:0 */ int my_uri_parse( apr_pool_t *pool , const char *uri_str , apr_uri_t *uri ) { apr_status_t rv = APR_SUCCESS; rv = apr_uri_parse(pool, uri_str, uri); if (rv != APR_SUCCESS) { return 0; } /* URI スキームは "http" か "https" 限定 */ if ((!uri->scheme) || (uri->scheme && (strcmp(uri->scheme, "http") && strcmp(uri->scheme, "https"))) ) { return 0; } /* ポート番号の補完 */ if (!uri->port_str) { /* URI にポート番号がない場合には、uri.port_str に値はセットされない。 */ /* ここでは、スキーマ文字列からデフォルトポートを推測している。 */ uri->port = apr_uri_port_of_scheme(uri->scheme); uri->port_str = apr_itoa(pool, uri->port); } /* パスの補完 */ if (!uri->path) { /* URI にパスが指定されていない場合は "/" で補完する。 */ uri->path = "/"; } /* 最終的にホストネームとポート番号、パスを取得できたか確認 */ if (!uri->hostname || uri->port <= 0 || !uri->path) { return 0; } return 1; }