OSDN Git Service

hostnamep, http-date function.
authorvisor <visor@users.sourceforge.jp>
Wed, 9 Jul 2014 14:35:23 +0000 (23:35 +0900)
committervisor <visor@users.sourceforge.jp>
Wed, 9 Jul 2014 14:35:23 +0000 (23:35 +0900)
lib/expr.cc
lib/expr.h
lib/ml.h
modules/ml-http.cc
modules/ml-http.h

index b0f86d7..ac8a1d3 100644 (file)
@@ -107,6 +107,12 @@ int  eval_int (MNode* cell, MlEnv* mlenv) {
     return to_int (p ());
 }
 
+int64_t  eval_int64 (MNode* cell, MlEnv* mlenv) {
+    MNodePtr  p;
+    p = eval (cell, mlenv);
+    return to_int64 (p ());
+}
+
 ustring  eval_str (MNode* cell, MlEnv* mlenv) {
     MNodePtr  p;
     p = eval (cell, mlenv);
index 8b42f73..e58cfb7 100644 (file)
@@ -17,6 +17,7 @@ typedef struct {
 MNode*  eval (MNode* ptr, MlEnv* mlenv);
 double  eval_double (MNode* ptr, MlEnv* mlenv);
 int  eval_int (MNode* ptr, MlEnv* mlenv);
+int64_t  eval_int64 (MNode* ptr, MlEnv* mlenv);
 ustring  eval_str (MNode* ptr, MlEnv* mlenv);
 ustring  eval_text1 (MNode* cell, MlEnv* mlenv);
 ustring  eval_asciiword (MNode* cell, MlEnv* mlenv);
index 3163e19..a96b144 100644 (file)
--- a/lib/ml.h
+++ b/lib/ml.h
@@ -148,6 +148,9 @@ class  MNode {
     int  to_int () {
        return (int)to_double ();
     };
+    int64_t  to_int64 () {
+       return (int64_t)to_double ();
+    };
     bool  to_bool ();
     ustring  cons_to_sexp (bool fcdr = false);
     ustring  to_sexp ();
@@ -173,7 +176,15 @@ inline double  to_double (MNode* c) {
 
 inline int  to_int (MNode* c) {
     if (c) {
-       return (int)(c->to_double ());
+       return c->to_int ();
+    } else {
+       return 0;
+    }
+}
+
+inline int64_t  to_int64 (MNode* c) {
+    if (c) {
+       return c->to_int64 ();
     } else {
        return 0;
     }
index c92f133..3038c9e 100644 (file)
@@ -12,6 +12,7 @@
 #include "ustring.h"
 #include <iostream>
 #include <fstream>
+#include <time.h>
 
 static void  location_sub (ustring& url, MNode* query, MlEnv* mlenv) {
     upair  scheme, delim, user, pass, host, port, rest;
@@ -512,6 +513,52 @@ MNode*  ml_build_query (MNode* cell, MlEnv* mlenv) {
 }
 
 /*DOC:
+===http-date===
+ (http-date INTEGER) -> STRING
+
+*/
+//#AFUNC       http-date       ml_http_date
+MNode*  ml_http_date (MNode* cell, MlEnv* mlenv) {
+    MNode*  arg = cell->cdr ();
+    time_t  tm;
+    struct tm  tms;
+    char  b[64];
+    size_t  s;
+
+    if (!arg)
+       throw (uErrorWrongNumber);
+    tm = eval_int64 (arg->car (), mlenv);
+    nextNode (arg);
+    if (arg)
+       throw (uErrorWrongNumber);
+
+    gmtime_r (&tm, &tms);
+    s = strftime (b, 64, "%a, %d %b %Y %H:%M:%S GMT", &tms);
+
+    return newMNode_str (new ustring (b, s));
+}
+
+/*DOC:
+===hostnamep===
+ (hostnamep STRING) -> BOOL
+
+*/
+//#AFUNC       hostnamep       ml_hostnamep
+MNode*  ml_hostnamep (MNode* cell, MlEnv* mlenv) {
+    MNode*  arg = cell->cdr ();
+    ustring  hostname;
+
+    if (!arg)
+       throw (uErrorWrongNumber);
+    hostname = eval_str (arg->car (), mlenv);
+    nextNode (arg);
+    if (arg)
+       throw (uErrorWrongNumber);
+
+    return newMNode_bool (checkHostname (hostname));
+}
+
+/*DOC:
 ===$http-get==
  ($http-get URL [#get] [#post] [#put] [#delete] [#file] [#head]
        [:id STRING] [:password STRING | :pw STRING]
@@ -958,3 +1005,4 @@ MNode*  ml_http_get_header_all (MNode* cell, MlEnv* mlenv, MLFunc* mobj) {
 
     return ans.release ();
 }
+
index a26825e..cd580b6 100644 (file)
@@ -39,6 +39,8 @@ MNode*  ml_response_no_cache (MNode* cell, MlEnv* mlenv);
 MNode*  ml_build_url (MNode* cell, MlEnv* mlenv);
 MNode*  ml_build_url_path (MNode* cell, MlEnv* mlenv);
 MNode*  ml_build_query (MNode* cell, MlEnv* mlenv);
+MNode*  ml_http_date (MNode* cell, MlEnv* mlenv);
+MNode*  ml_hostnamep (MNode* cell, MlEnv* mlenv);
 MNode*  ml_http_get (MNode* cell, MlEnv* mlenv);
 MNode*  ml_http_get_http_status (MNode* cell, MlEnv* mlenv, MLFunc* mobj);
 MNode*  ml_http_get_http_response (MNode* cell, MlEnv* mlenv, MLFunc* mobj);