Ignore:
Timestamp:
01/29/16 13:18:53 (9 years ago)
Author:
tbretz
Message:
Added the curl object to allow for simple http requests.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/InterpreterV8.cc

    r18383 r18426  
    1010
    1111#include <boost/tokenizer.hpp>
     12#include <boost/algorithm/string/join.hpp>
    1213
    1314#ifdef HAVE_NOVA
     
    926927    const Locker lock;
    927928    return handle_scope.Close(Integer::New(WEXITSTATUS(rc)));
     929}
     930
     931// ==========================================================================
     932//                                 Curl
     933// ==========================================================================
     934
     935Handle<Value> InterpreterV8::ConstructorCurl(const Arguments &args)
     936{
     937    if (!args.IsConstructCall())
     938        return ThrowException(String::New("Curl must be called as constructor"));
     939
     940    if (args.Length()!=1 || !args[0]->IsString())
     941        return ThrowException(String::New("Constructor must be called with a single string as argument"));
     942
     943    HandleScope handle_scope;
     944
     945    Handle<Array> data = Array::New();
     946    if (data.IsEmpty())
     947        return Undefined();
     948
     949    Handle<Object> self = args.This();
     950
     951    self->Set(String::New("url"),  args[0]->ToString(), ReadOnly);
     952    self->Set(String::New("data"), data, ReadOnly);
     953
     954    self->Set(String::New("send"), FunctionTemplate::New(WrapSendCurl)->GetFunction(), ReadOnly);
     955
     956    return handle_scope.Close(self);
     957}
     958
     959Handle<Value> InterpreterV8::FuncSendCurl(const Arguments& args)
     960{
     961    HandleScope handle_scope;
     962
     963    if (args.Length()>1)
     964        return ThrowException(String::New("Only one argument allowed."));
     965
     966    if (args.Length()==1 && !args[0]->IsBoolean())
     967        return ThrowException(String::New("Argument must be a boolean."));
     968
     969    const bool block = args.Length()==0 || args[0]->BooleanValue();
     970
     971    const Handle<Value> url  = args.This()->Get(String::New("url"));
     972    const Handle<Value> data = args.This()->Get(String::New("data"));
     973
     974    const vector<string> vdata = ValueToArray(data);
     975    const string sdata = boost::algorithm::join(vdata, "&");
     976
     977    const string surl = *String::AsciiValue(url->ToString());
     978
     979    string cmd = "curl -sSf ";
     980    if (!sdata.empty())
     981        cmd += "--data '"+sdata+"' ";
     982    cmd += "'http://"+surl+"' 2>&1 ";
     983
     984    FILE *pipe = popen(cmd.c_str(), "r");
     985    if (!pipe)
     986        return ThrowException(String::New(strerror(errno)));
     987
     988    if (!block)
     989        return Undefined();
     990
     991    string txt;
     992
     993    while (!feof(pipe))
     994    {
     995        char buf[1025];
     996        if (fgets(buf, 1024, pipe)==NULL)
     997            break;
     998        txt += buf;
     999    }
     1000
     1001    const int rc = pclose(pipe);
     1002
     1003    Handle<Object> obj = Object::New();
     1004
     1005    obj->Set(String::New("cmd"), String::New(cmd.c_str()));
     1006    obj->Set(String::New("data"), String::New(txt.c_str()));
     1007    obj->Set(String::New("rc"), Integer::NewFromUnsigned(WEXITSTATUS(rc)));
     1008
     1009    const Locker lock;
     1010    return handle_scope.Close(obj);
    9281011}
    9291012
     
    25692652#endif
    25702653
     2654#ifdef HAVE_CURL
     2655    Handle<FunctionTemplate> curl = FunctionTemplate::New(ConstructorCurl);
     2656    mail->SetClassName(String::New("Curl"));
     2657    global->Set(String::New("Curl"), curl, ReadOnly);
     2658#endif
     2659
    25712660#ifdef HAVE_NOVA
    25722661    Handle<FunctionTemplate> sky = FunctionTemplate::New(ConstructorSky);
     
    28052894#endif
    28062895
     2896#ifdef HAVE_CURL
     2897    rc.emplace_back("Curl(");
     2898    rc.emplace_back("new Curl(");
     2899
     2900    rc.emplace_back(".url");
     2901    rc.emplace_back(".user");
     2902    rc.emplace_back(".data");
     2903//    rc.emplace_back(".send("); -> MAILX
     2904#endif
     2905
    28072906#ifdef HAVE_NOVA
    28082907    rc.emplace_back("Sky(");
Note: See TracChangeset for help on using the changeset viewer.