OSDN Git Service

get-http-header function.
authorvisor <visor@users.sourceforge.jp>
Fri, 17 Feb 2012 10:15:03 +0000 (19:15 +0900)
committervisor <visor@users.sourceforge.jp>
Fri, 17 Feb 2012 10:15:03 +0000 (19:15 +0900)
lib/util_check.cc
lib/util_check.h
lib/util_string.cc
lib/util_string.h
modules/ml-apache.cc
modules/ml-apache.h
modules/ml-string.cc
modules/ml-string.h

index 939b7a4..8f8f260 100644 (file)
@@ -5,7 +5,7 @@
 #include "motorconst.h"
 #include "ustring.h"
 
-bool  checkRe (const ustring& name, uregex& re) {
+bool  checkRe (const ustring& name, const uregex& re) {
     umatch  m;
 
     if (usearch (name, m, re)) {
@@ -15,7 +15,7 @@ bool  checkRe (const ustring& name, uregex& re) {
     }
 }
 
-bool  checkRe (uiterator& b, uiterator& e, uregex& re) {
+bool  checkRe (const uiterator& b, const uiterator& e, const uregex& re) {
     umatch  m;
 
     if (usearch (b, e, m, re)) {
@@ -44,15 +44,12 @@ bool  checkResourceName (const ustring& name) {
 }
 
 bool  checkAbsoluteResourceName (const ustring& name) {
-//    static uregex  re ("^/(" kWNAME "{0,127}/)*" kWNAME "{0,127}(\\." kWORD "{1,16})?$");
     static uregex  re ("^/(" kFName "/)*" kFName "$");
 
     return (checkRe (name, re));
 }
 
 bool  checkASCII (const ustring& name) {
-//    static uregex  re ("^[ -\\x7e]*$");
-//    return (checkRe (name, re));
     static uregex  re ("[^ -\\x7e]");
 
     return (! checkRe (name, re));
@@ -88,39 +85,48 @@ bool  checkMailAddr (const ustring& name) {
     return (checkRe (name, re));
 }
 
+bool  checkAlNum (const uiterator& b, const uiterator& e) {
+    static  uregex re ("^[a-zA-Z_0-9]+$");
+
+    return (checkRe (b, e, re));
+}
+
 bool  checkNum (const ustring& text) {
-    return (checkRe (text, re_num));
+    return (checkNum (text.begin (), text.end ()));
 }
 
-bool  checkNum (uiterator& b, uiterator& e) {
+bool  checkNum (const uiterator& b, const uiterator& e) {
     return (checkRe (b, e, re_num));
 }
 
-static uregex  re_width ("^[0-9]+(\\.[0-9]+)?(%|px|pt|in|mm|cm|em|ex)?$");
-bool  checkWidth (uiterator& b, uiterator& e) {
+bool  checkWidth (const uiterator& b, const uiterator& e) {
+    static uregex  re_width ("^[0-9]+(\\.[0-9]+)?(%|px|pt|in|mm|cm|em|ex)?$");
     return (checkRe (b, e, re_width));
 }
 
 bool  checkWidth (const ustring& text) {
-    return (checkRe (text, re_width));
+//    return (checkRe (text, re_width));
+    return (checkWidth (text.begin (), text.end ()));
 }
 
-static uregex  re_color ("^#([0-9a-fA-F]{3}){1,2}$");
-bool  checkColor (uiterator& b, uiterator& e) {
+bool  checkColor (const uiterator& b, const uiterator& e) {
+    static uregex  re_color ("^#([0-9a-fA-F]{3}){1,2}$");
     return (checkRe (b, e, re_color));
 }
 
 bool  checkColor (const ustring& text) {
-    return (checkRe (text, re_color));
+//    return (checkRe (text, re_color));
+    return (checkColor (text.begin (), text.end ()));
 }
 
-static uregex  re_wikiid ("^" rWikiID "$");
-bool  checkWikiID (uiterator& b, uiterator& e) {
+bool  checkWikiID (const uiterator& b, const uiterator& e) {
+    static uregex  re_wikiid ("^" rWikiID "$");
     return (checkRe (b, e, re_wikiid));
 }
 
 bool  checkWikiID (const ustring& text) {
-    return (checkRe (text, re_wikiid));
+//    return (checkRe (text, re_wikiid));
+    return (checkWikiID (text.begin (), text.end ()));
 }
 
 bool  checkAry (const ustring& name) {
index 901c22b..748ef69 100644 (file)
@@ -12,8 +12,8 @@
 #define  UA_Opera      0x00000800
 #define  UA_NetFront   0x00001000
 
-bool  checkRe (const ustring& name, uregex& re);
-bool  checkRe (uiterator& b, uiterator& e, uregex& re);
+bool  checkRe (const ustring& name, const uregex& re);
+bool  checkRe (const uiterator& b, const uiterator& e, const uregex& re);
 bool  checkName (const ustring& name);
 bool  checkFilename (const ustring& name);
 bool  checkResourceName (const ustring& name);
@@ -24,13 +24,14 @@ bool  checkDomain_dot (const ustring& name);
 bool  checkHostname (const ustring& name);
 bool  checkMimeType (const ustring& name);
 bool  checkMailAddr (const ustring& name);
+bool  checkAlNum (const uiterator& b, const uiterator& e);
 bool  checkNum (const ustring& text);
-bool  checkNum (uiterator& b, uiterator& e);
-bool  checkWidth (uiterator& b, uiterator& e);
+bool  checkNum (const uiterator& b, const uiterator& e);
+bool  checkWidth (const uiterator& b, const uiterator& e);
 bool  checkWidth (const ustring& text);
 bool  checkColor (uiterator& b, uiterator& e);
 bool  checkColor (const ustring& text);
-bool  checkWikiID (uiterator& b, uiterator& e);
+bool  checkWikiID (const uiterator& b, const uiterator& e);
 bool  checkWikiID (const ustring& text);
 bool  checkAry (const ustring& name);
 bool  checkAry (const ustring& name, ustring& sym);
index c315dba..100e771 100644 (file)
@@ -10,6 +10,7 @@
 #include "utf16.h"
 #include <boost/regex.hpp>
 #include <boost/regex/pattern_except.hpp>
+#include <boost/algorithm/string.hpp>
 #include <vector>
 #include <algorithm>
 #include <stdlib.h>
@@ -1243,3 +1244,10 @@ ustring  formatDateString (const ustring& format, time_t tm) {
     return ans;
 }
 
+ustring  toLower (const ustring& str) {
+    return boost::to_lower_copy (str);
+}
+
+ustring  toUpper (const ustring& str) {
+    return boost::to_upper_copy (str);
+}
index cc18499..ee26335 100644 (file)
@@ -106,5 +106,7 @@ ustring  toLower (uiterator b, uiterator e);
 class  MNodePtr;
 ustring  formatString (const ustring& format, boost::ptr_vector<MNodePtr>& par);
 ustring  formatDateString (const ustring& format, time_t tm);
+ustring  toLower (const ustring& str);
+ustring  toUpper (const ustring& str);
 
 #endif /* UTIL_STRING_H */
index e951bfd..a5001dd 100644 (file)
@@ -3,6 +3,7 @@
 #include "mlenv.h"
 #include "motorenv.h"
 #include "formfile.h"
+#include "utf8.h"
 #include "util_apache.h"
 #include "util_check.h"
 #include "util_const.h"
@@ -25,7 +26,8 @@ static MNode*  env_sub (MNode* cell, const char* name) {
        char*  e = getenv (name);
        
        if (e) {
-           return newMNode_str (new ustring (e));
+           ustring  s (e);
+           return newMNode_str (new ustring (fixUTF8 (s)));
        } else {
            return NULL;
        }
@@ -139,6 +141,46 @@ MNode*  ml_ssl_client_i_dn (MNode* cell, MlEnv* mlenv) {
 }
 
 /*DOC:
+===get-http-header===
+ (get-http-header NAME) -> STRING
+
+*/
+//#AFUNC       get-http-header ml_get_http_header
+MNode*  ml_get_http_header (MNode* cell, MlEnv* mlenv) {
+    MNode*  arg = cell->cdr ();
+    ustring  name;
+    ustring::iterator  b, e;
+
+    if (! arg)
+       throw (uErrorWrongNumber);
+    name = toUpper (eval_text1 (arg->car (), mlenv));
+    nextNode (arg);
+    if (arg)
+       throw (uErrorWrongNumber);
+
+    b = name.begin ();
+    e = name.end ();
+    for (; b != e; b ++) {
+       if (*b == '-') {
+           *b = '_';
+       }
+    }
+
+    if (checkAlNum (name.begin (), e) && name.length () < 128) {
+       name = ustring (CharConst ("HTTP_")).append (name);
+       char*  e = getenv (name.c_str ());
+       if (e) {
+           ustring  s (e);
+           return newMNode_str (new ustring (fixUTF8 (s)));
+       } else {
+           return NULL;
+       }
+    } else {
+       return NULL;
+    }
+}
+
+/*DOC:
 ===is-get-method===
  (is-get-method) -> BOOL
 
index f947113..cea1870 100644 (file)
@@ -18,6 +18,7 @@ MNode*  ml_apache_https (MNode* cell, MlEnv* mlenv);
 MNode*  ml_ssl_client_m_serial (MNode* cell, MlEnv* mlenv);
 MNode*  ml_ssl_client_s_dn (MNode* cell, MlEnv* mlenv);
 MNode*  ml_ssl_client_i_dn (MNode* cell, MlEnv* mlenv);
+MNode*  ml_get_http_header (MNode* cell, MlEnv* mlenv);
 MNode*  ml_is_get_method (MNode* cell, MlEnv* mlenv);
 MNode*  ml_is_post_method (MNode* cell, MlEnv* mlenv);
 MNode*  ml_absolute_url (MNode* cell, MlEnv* mlenv);
index f7f62cf..319b241 100644 (file)
@@ -915,3 +915,45 @@ MNode*  ml_sort_string (MNode* cell, MlEnv* mlenv) {
     }
     return ans.release ();
 }
+
+/*DOC:
+===to-upper===
+ (to-upper STRING) -> STRING
+
+*/
+//#AFUNC       to-upper        ml_to_upper
+//#WIKIFUNC    to-upper
+MNode*  ml_to_upper (MNode* cell, MlEnv* mlenv) {
+    MNode*  arg = cell->cdr ();
+    ustring  text;
+
+    if (! arg)
+       throw (uErrorWrongNumber);
+    text = eval_str (arg->car (), mlenv);
+    nextNode (arg);
+    if (arg)
+       throw (uErrorWrongNumber);
+
+    return newMNode_str (new ustring (toUpper (text)));
+}
+
+/*DOC:
+===to-lower===
+ (to-lower STRING) -> STRING
+
+*/
+//#AFUNC       to-lower        ml_to_lower
+//#WIKIFUNC    to-lower
+MNode*  ml_to_lower (MNode* cell, MlEnv* mlenv) {
+    MNode*  arg = cell->cdr ();
+    ustring  text;
+
+    if (! arg)
+       throw (uErrorWrongNumber);
+    text = eval_str (arg->car (), mlenv);
+    nextNode (arg);
+    if (arg)
+       throw (uErrorWrongNumber);
+
+    return newMNode_str (new ustring (toLower (text)));
+}
index 0a384bb..8a3b625 100644 (file)
@@ -29,5 +29,7 @@ MNode*  ml_to_string (MNode* cell, MlEnv* mlenv);
 MNode*  ml_to_sexp (MNode* cell, MlEnv* mlenv);
 MNode*  ml_is_ascii63 (MNode* cell, MlEnv* mlenv);
 MNode*  ml_sort_string (MNode* cell, MlEnv* mlenv);
+MNode*  ml_to_upper (MNode* cell, MlEnv* mlenv);
+MNode*  ml_to_lower (MNode* cell, MlEnv* mlenv);
 
 #endif /* ML_STRING_H */