Example #1
0
Json::Value make_rpc_req(Json::Value query, bool logit,unsigned char host,int timeout)
{
	HTTPClient cl;
	cl.rh["Authorization"] = hosts[host].aut;
	cl.rh["Connection"] = "close";
	Json::FastWriter writer;
	cl.pr["_full_post"] = writer.write(query);
	if (logit) {
		FILE *fd = fopen(".rpc.log", "w"); // log only 1 transaction
		if (fd) {
			fprintf(fd, "COMPLETE RPC REQUEST:\n%s\nEND OF REQUEST\n", cl.pr["_full_post"].c_str());
			fclose(fd);
		}
	}
        if(timeout){
                signal(SIGALRM,connect_alarm);
                alarm(timeout);
                cl.request(hosts[host].url,true);
                alarm(0);}
        else{
                cl.request(hosts[host].url,true);}
	if (!cl.isOK()) {
		return Json::Value();
	}
	std::string ans;
	while (cl.peek() != EOF) {
		unsigned to_r = cl.rdbuf()->in_avail();
		if (to_r == 0) break;
		if (to_r > 4000) to_r = 4000;
		char tbuf[to_r+2];
		cl.read(tbuf,to_r);
		ans += std::string(tbuf, to_r);
	}
	if (logit) {
		FILE *fd = fopen(".rpc.log", "a");
		if (fd) {
			fprintf(fd, "COMPLETE RPC ANSWER:\n%s\nEND OF ANSWER\n", ans.c_str());
			fclose(fd);
		}
	}
	cl.disconnect();
	Json::Reader reader;
	Json::Value answ;
	if (!reader.parse(ans, answ)) return false;
	if (answ.type() != Json::objectValue) return false;
	answ = answ["result"];
	if (answ.type() != Json::objectValue) return false;
	return answ;
}
Example #2
0
int
HTTPClient::clientRead(FILE* stream)
{
  if (stream == NULL)
    {
      return EOF;
    }
  http_stream_udata* udata = (http_stream_udata*) fdev_get_udata(stream);
  HTTPClient* client = udata->client;
  if (!client->connected())
    {
      return EOF;
    }
  //block until we got a byte
  while (client->available() == 0)
    {
      if (client->connected() == 0)
        {
          return EOF;
        }
    };
  int result = client->read();
  if (result == EOF)
    {
      return EOF;
    }
  if (client->debugCommunication)
    {
      Serial.print((byte) result);
    }
  //as long as we do not read encoded or it is no % everything is ok
  if (udata->encode == 0 || result != '%')
    {
      return result;
    }
  else
    {
      //block until we got the needed bytes
      while (client->available() >= 2)
        {
          if (client->connected() == 0)
            {
              return EOF;
            }
        };
      char return_value = 0;
      for (char i = 0; i < 2; i++)
        {
          result = client->read();
          if (result == EOF)
            {
              return EOF;
            }
          else if (result >= 'A' && result <= 'Z')
            {
              return_value += (1 - i) * 16 * (result - 'A');
            }
          else if (result >= 'a' && result <= 'z')
            {
              return_value += (1 - i) * 16 * (result - 'a');
            }
          else if (result >= '0' && result <= '9')
            {
              return_value += (1 - i) * 16 * (result - '0');
            }
        }
      return return_value;
    }
}