OSDN Git Service

34ac37154e5b58e654bceb2b8ebfdbae79c5f91c
[hmh/hhml.git] / lib / util_apache.cc
1 #include "util_apache.h"
2 #include "util_const.h"
3 #include "util_file.h"
4 #include "util_splitter.h"
5 #include "util_string.h"
6 #include "httpconst.h"
7 #include "ustring.h"
8 #include <vector>
9
10 ustring  apacheAbsolutePath (const ustring& url) {
11     ustring  ans;
12     std::vector<ustring>  ary;
13     std::vector<ustring>::iterator  it;
14     Splitter  sp (url.begin (), url.end (), re_slash);
15     uiterator  b, e;
16     bool  fdirpath;
17     size_t  len = url.length ();
18
19     if (len == 0 || url[len - 1] == '/') {
20         fdirpath = true;
21     } else {
22         fdirpath = false;
23     }
24     if (isAbsolutePath (url)) {
25         sp.next ();
26     } else {
27 /*      ustring  e = getenvString (kSCRIPT_NAME);
28         ustring  p = getenvString (kPATH_INFO);
29         if (p.length () > 0) e.append (p);
30 */
31         // mod_rewriteで書き換えた時、元のURLを返す
32         ustring  e = getenvString (kREQUEST_URI);
33         // REQUEST_URIはデコードされていない。
34         ustring::size_type  p = e.find_last_of ('?');
35         if (p != ustring::npos)
36             e.resize (p);
37         e = percentDecode (e);  // ディレクトリパスをチェック前にデコードする。
38         splitE (e.begin (), e.end (), re_slash, ary);
39         if (ary.size () > 0 && ary.back ().length () > 0) {
40             ary.pop_back();
41         }
42     }
43     while (sp.next ()) {
44         b = sp.begin ();
45         e = sp.end ();
46         if (b == e) {
47         } else if (match (b, e, CharConst ("."))) {
48         } else if (match (b, e, CharConst (".."))) {
49             if (ary.size () > 0)
50                 ary.pop_back ();
51         } else {
52             ary.push_back (ustring (b, e));
53         }
54     }
55
56     for (it = ary.begin (); it != ary.end (); it ++) {
57         if ((*it).length () > 0) {
58             ans.append (uSlash).append (*it);
59         }
60     }
61     if (fdirpath) {
62         ans.append (uSlash);
63     }
64
65     return ans;
66 }