Ejemplo n.º 1
0
std::unique_ptr<HTTPMessage> getRequest(HTTPMethod type) {
  auto req = folly::make_unique<HTTPMessage>();
  req->setMethod(type);
  req->setHTTPVersion(1, 1);
  req->setURL("/");
  return req;
}
Ejemplo n.º 2
0
std::unique_ptr<HTTPMessage> makeResponse(uint16_t statusCode) {
  auto resp = std::make_unique<HTTPMessage>();
  resp->setStatusCode(statusCode);
  resp->setHTTPVersion(1, 1);
  return resp;
}
Ejemplo n.º 3
0
int QoreHttpClientObject::setOptions(const QoreHashNode* opts, ExceptionSink* xsink) {
   // process new protocols
   const AbstractQoreNode* n = opts->getKeyValue("protocols");  

   if (n && n->getType() == NT_HASH) {
      const QoreHashNode* h = reinterpret_cast<const QoreHashNode* >(n);
      ConstHashIterator hi(h);
      while (hi.next()) {
	 const AbstractQoreNode* v = hi.getValue();
	 qore_type_t vtype = v ? v->getType() : 0;
	 if (!v || (vtype != NT_HASH && vtype != NT_INT)) {
	    xsink->raiseException("HTTP-CLIENT-OPTION-ERROR", "value of protocol hash key '%s' is not a hash or an int", hi.getKey());
	    return -1;
	 }
	 bool need_ssl = false;
	 int need_port;
	 if (vtype == NT_INT)
	    need_port = (int)((reinterpret_cast<const QoreBigIntNode* >(v))->val);
	 else {
	    const QoreHashNode* vh = reinterpret_cast<const QoreHashNode* >(v);
	    const AbstractQoreNode* p = vh->getKeyValue("port");
	    need_port = p ? p->getAsInt() : 0;
	    if (!need_port) {
	       xsink->raiseException("HTTP-CLIENT-OPTION-ERROR", "'port' key in protocol hash key '%s' is missing or zero", hi.getKey());
	       return -1;
	    }
	    p = vh->getKeyValue("ssl");
	    need_ssl = p ? p->getAsBool() : false;
	 }
	 http_priv->prot_map[hi.getKey()] = make_protocol(need_port, need_ssl);
      }
   }

   n = opts->getKeyValue("max_redirects");
   if (n)
      http_priv->max_redirects = n->getAsInt();

   n = opts->getKeyValue("default_port");  
   if (n)
      http_priv->default_port = n->getAsInt();
   else
      http_priv->default_port = HTTPCLIENT_DEFAULT_PORT;

   // check if proxy is true
   n = opts->getKeyValue("proxy"); 
   if (n && n->getType() == NT_STRING)
      if (http_priv->set_proxy_url_unlocked((reinterpret_cast<const QoreStringNode* >(n))->getBuffer(), xsink))
	 return -1;

   // parse url option if present
   n = opts->getKeyValue("url");  
   if (n && n->getType() == NT_STRING)
      if (http_priv->set_url_unlocked((reinterpret_cast<const QoreStringNode* >(n))->getBuffer(), xsink))
	 return -1;

   n = opts->getKeyValue("default_path");  
   if (n && n->getType() == NT_STRING)
      http_priv->default_path = (reinterpret_cast<const QoreStringNode* >(n))->getBuffer();

   // set default timeout if given in option hash - accept relative date/time values as well as integers
   n = opts->getKeyValue("timeout");  
   if (n)
      http_priv->timeout = getMsZeroInt(n);

   n = opts->getKeyValue("http_version");  
   if (n) {
      if (n->getType() == NT_STRING) {
	 if (setHTTPVersion((reinterpret_cast<const QoreStringNode* >(n))->getBuffer(), xsink))
	    return -1;
      }
      else {
	 xsink->raiseException("HTTP-CLIENT-OPTION-ERROR", "expecting string version ('1.0', '1.1' as value for http_version key in options hash");
	 return -1;
      }
   }

   n = opts->getKeyValue("event_queue");
   if (n) {
       const QoreObject *o = n->getType() == NT_OBJECT ? reinterpret_cast<const QoreObject *>(n) : 0;
       Queue *q = o ? (Queue *)o->getReferencedPrivateData(CID_QUEUE, xsink) : 0;
       if (*xsink)
	   return -1;

       if (q) { // pass reference from QoreObject::getReferencedPrivateData() to function
	   priv->socket->setEventQueue(q, xsink);
       }
   }

   http_priv->connect_timeout_ms = getMsMinusOneInt(opts->getKeyValue("connect_timeout"));

   if (http_priv->connection.path.empty())
      http_priv->connection.path = http_priv->default_path.empty() ? "/" : http_priv->default_path;

   // additional HTTP methods for customized extensions like WebDAV
   n = opts->getKeyValue("additional_methods");
   if (n) {
       const QoreHashNode *h = n->getType() == NT_HASH ? reinterpret_cast<const QoreHashNode*>(n) : 0;
       if (!h) {
           xsink->raiseException("HTTP-CLIENT-OPTION-ERROR", "Option additional_methods requires a hash as a value; got: %s", n->getTypeName());
           return -1;
       }
       ConstHashIterator hi(h);
       while (hi.next()) {
           http_priv->addHttpMethod(hi.getKey(), hi.getValue()->getAsBool());
       }
   }

   return 0;
}