Exemplo n.º 1
0
void
tr_deepLog( const char  * file,
            int           line,
            const char  * name,
            const char  * fmt,
            ... )
{
    FILE * fp = tr_getLog( );
    if( fp || IsDebuggerPresent( ) )
    {
        va_list           args;
        char              timestr[64];
        struct evbuffer * buf = evbuffer_new( );
        char *            base = tr_basename( file );

        evbuffer_add_printf( buf, "[%s] ",
                            tr_getLogTimeStr( timestr, sizeof( timestr ) ) );
        if( name )
            evbuffer_add_printf( buf, "%s ", name );
        va_start( args, fmt );
        evbuffer_add_vprintf( buf, fmt, args );
        va_end( args );
        evbuffer_add_printf( buf, " (%s:%d)\n", base, line );
        /* FIXME(libevent2) ifdef this out for nonwindows platforms */
        OutputDebugString( EVBUFFER_DATA( buf ) );
        if(fp) /* FIXME(libevent2) tr_getLog() should return an fd, then use evbuffer_write() here ) */
            (void) fwrite( EVBUFFER_DATA( buf ), 1, EVBUFFER_LENGTH( buf ), fp );

        tr_free( base );
        evbuffer_free( buf );
    }
}
Exemplo n.º 2
0
void
http_post_cb(struct evhttp_request *req, void *arg)
{
	struct evbuffer *evb;
	event_debug(("%s: called\n", __func__));

	/* Yes, we are expecting a post request */
	if (req->type != EVHTTP_REQ_POST) {
		fprintf(stdout, "FAILED (post type)\n");
		exit(1);
	}

	if (EVBUFFER_LENGTH(req->input_buffer) != strlen(POST_DATA)) {
		fprintf(stdout, "FAILED (length: %zu vs %zu)\n",
		    EVBUFFER_LENGTH(req->input_buffer), strlen(POST_DATA));
		exit(1);
	}

	if (memcmp(EVBUFFER_DATA(req->input_buffer), POST_DATA,
		strlen(POST_DATA))) {
		fprintf(stdout, "FAILED (data)\n");
		fprintf(stdout, "Got :%s\n", EVBUFFER_DATA(req->input_buffer));
		fprintf(stdout, "Want:%s\n", POST_DATA);
		exit(1);
	}
	
	evb = evbuffer_new();
	evbuffer_add_printf(evb, "This is funny");

	evhttp_send_reply(req, HTTP_OK, "Everything is fine", evb);

	evbuffer_free(evb);
}
Exemplo n.º 3
0
const void *LibEventTransport::getMorePostData(int &size) {
#ifdef EVHTTP_READ_LIMITING
  if (m_request->ntoread == 0) {
    size = 0;
    return NULL;
  }

  evbuffer *buf = m_request->input_buffer;
  ASSERT(buf);
  evbuffer_drain(buf, EVBUFFER_LENGTH(buf));

  if (evhttp_get_more_post_data(m_request, &m_epollfd, &m_epollevent)) {
    buf = m_request->input_buffer;
    ASSERT(buf);
    size = EVBUFFER_LENGTH(buf);
    evbuffer_expand(buf, size + 1); // allowing NULL termination
    // EVBUFFER_DATA(buf) might change after evbuffer_expand
    ((char*)EVBUFFER_DATA(buf))[size] = '\0';
    if (m_request->ntoread == 0) {
      evhttp_get_post_data_done(m_request);
    }
    return EVBUFFER_DATA(buf);
  }
  if (m_epollfd != -1) {
    close(m_epollfd);
    m_epollfd = -1;
  }
  evhttp_get_post_data_done(m_request);
  size = 0;
  return NULL;
#else
  size = 0;
  return NULL;
#endif
}
Exemplo n.º 4
0
static void bufevent_readcb(struct bufevent *bufev, void *arg)
{
	struct json_rpc_tt *jt = (struct json_rpc_tt *)arg;
	struct jrpc_bufevent *jb = (struct jrpc_bufevent *)jt->impl;

	struct evbuffer *buf = bufevent_get_input(bufev);
	json_parser_process(jb->jp, (char *)EVBUFFER_DATA(buf), (char *)EVBUFFER_DATA(buf) + EVBUFFER_LENGTH(buf));
}
Exemplo n.º 5
0
void RequestSend(Server *pServer, u32 type, struct evbuffer *evSend) {
    struct Poll *poll = pServer->poll;
    //#define COMPRESS
    struct evbuffer *evReq = evbuffer_new();
    struct Request Req, *pReq;
    bzero((char *) & Req, Size_Request);

    evbuffer_add(evReq, (char *) & Req, Size_Request);

    if (evSend > 0) {
        evbuffer_add_buffer(evReq, evSend);
    }

    debug("len:%d to %s:%d 0x%08x", evbuffer_get_length(evReq), pServer->host, pServer->port, pServer->poll->bev);

    pReq = (struct Request *) (EVBUFFER_DATA(evReq));
    pReq->sizes.UncmprSize = evbuffer_get_length(evReq) - Size_Request_sizes;
    pReq->hdr.TesterId = config.testerid;
    pReq->hdr.ReqType = type;


#ifdef COMPRESS
    if (pReq->sizes.UncmprSize > 200) {
        int lenAlloc = (pReq->sizes.UncmprSize)*1.01 + 12 + Size_Request_sizes;
        int lenCmpr = lenAlloc - Size_Request_sizes;
        char *ptrCmpr;
        ptrCmpr = getNulledMemory(lenAlloc);
        memcpy(ptrCmpr, pReq, Size_Request_sizes);
        compress2((Bytef *) ptrCmpr + Size_Request_sizes, (uLongf *) & lenCmpr, (Bytef *) & pReq->hdr, evbuffer_get_length(evReq) - Size_Request_sizes, Z_DEFAULT_COMPRESSION);
        evbuffer_free(evReq);

        evReq = evbuffer_new();
        evbuffer_add(evReq, ptrCmpr, lenCmpr + Size_Request_sizes);
        free(ptrCmpr);
        pReq = (struct Request *) EVBUFFER_DATA(evReq);
        pReq->sizes.CmprSize = lenCmpr;
    }

#endif
    pReq->sizes.crc = crc32(0xffffffff, (const Bytef *) pReq, evbuffer_get_length(evReq));
    /*
        #ifdef DEBUG
            printf(cBLUE"\treq->sizes.CmprSize=%d"cEND, pReq->sizes.CmprSize);
            printf(cBLUE"\treq->sizes.UncmprSize=%d"cEND, pReq->sizes.UncmprSize);
            printf(cBLUE"\treq->sizes.crc=0x%08x\n"cEND, pReq->sizes.crc);
        #ifdef HEXPRINT
            hexPrint((char *) EVBUFFER_DATA(evReq), evbuffer_get_length(evReq));
        #endif
        #endif
     */

    bufferevent_write_buffer(poll->bev, evReq);
    evbuffer_free(evReq);
}
Exemplo n.º 6
0
void simplehttp_log(const char *host, struct evhttp_request *req, uint64_t req_time, const char *id, int display_post)
{
    // NOTE: this is localtime not gmtime
    time_t now;
    struct tm *tm_now;
    char datetime_buf[64];
    char code;
    const char *method;
    char *uri;
    int response_code;
    int type;
    
    time(&now);
    tm_now = localtime(&now);
    strftime(datetime_buf, 64, "%y%m%d %H:%M:%S", tm_now);
    
    if (req) {
        if (req->response_code >= 500 && req->response_code < 600) {
            code = 'E';
        } else if (req->response_code >= 400 && req->response_code < 500) {
            code = 'W';
        } else {
            code = 'I';
        }
        response_code = req->response_code;
        method = simplehttp_method(req);
        uri = req->uri;
        type = req->type;
    } else {
        code = 'E';
        response_code = 0;
        method = "NA";
        uri = "";
        type = -1;
    }
    
    fprintf(stdout, "[%c %s %s] %d %s %s%s", code, datetime_buf, id, response_code, method, host, uri);
    
    if (display_post && (type == EVHTTP_REQ_POST)) {
        if (req->input_buffer == NULL || EVBUFFER_DATA(req->input_buffer) == NULL) {
            fprintf(stdout, "<ERROR req->input_buffer=%p, EVBUFFER_DATA=%p>", req->input_buffer, /* must be */ NULL);
        } else {
            fprintf(stdout, "?");
            fwrite(EVBUFFER_DATA(req->input_buffer), EVBUFFER_LENGTH(req->input_buffer), 1, stdout);
        }
    }
    
    fprintf(stdout, " %.3fms\n", req_time / 1000.0);
}
Exemplo n.º 7
0
char *
cmd_capture_pane_pending(struct args *args, struct window_pane *wp,
    size_t *len)
{
	char	*buf, *line, tmp[5];
	size_t	 linelen;
	u_int	 i;

	if (wp->ictx.since_ground == NULL)
		return (xstrdup(""));

	line = EVBUFFER_DATA(wp->ictx.since_ground);
	linelen = EVBUFFER_LENGTH(wp->ictx.since_ground);

	buf = xstrdup("");
	if (args_has(args, 'C')) {
		for (i = 0; i < linelen; i++) {
			if (line[i] >= ' ') {
				tmp[0] = line[i];
				tmp[1] = '\0';
			} else
				xsnprintf(tmp, sizeof tmp, "\\%03o", line[i]);
			buf = cmd_capture_pane_append(buf, len, tmp,
			    strlen(tmp));
		}
	} else
		buf = cmd_capture_pane_append(buf, len, line, linelen);
	return (buf);
}
Exemplo n.º 8
0
static char *
announceToScrape( const char * announce )
{
    char *       scrape = NULL;
    const char * s;

    /* To derive the scrape URL use the following steps:
     * Begin with the announce URL. Find the last '/' in it.
     * If the text immediately following that '/' isn't 'announce'
     * it will be taken as a sign that that tracker doesn't support
     * the scrape convention. If it does, substitute 'scrape' for
     * 'announce' to find the scrape page.  */
    if( ( ( s =
               strrchr( announce, '/' ) ) ) && !strncmp( ++s, "announce", 8 ) )
    {
        struct evbuffer * buf = tr_getBuffer( );
        evbuffer_add( buf, announce, s - announce );
        evbuffer_add( buf, "scrape", 6 );
        evbuffer_add_printf( buf, "%s", s + 8 );
        scrape = tr_strdup( EVBUFFER_DATA( buf ) );
        tr_releaseBuffer( buf );
    }

    return scrape;
}
Exemplo n.º 9
0
void
rrdtool_evb_readcb(struct bufferevent *bev, void *parameter)
{
	struct rrdtool_drv *req = parameter;

	char *start, *end;

	start = EVBUFFER_DATA(bev->input);
	if ((end = evbuffer_find(bev->input, "OK ", 3)) == NULL)
		return;
	
	/* Find the end of the line */
	if (strchr(end, '\n') == NULL)
		return;

	/* Communicate everything before the OK to the call back */
	*end = '\0';

	rrdtool_command_done(req, start);
	
	/* 
	 * We drain all the input because we do not currently interleave
	 * commands.
	 */
	evbuffer_drain(bev->input, -1);

	return;
}
Exemplo n.º 10
0
char *
evbuffer_readln_crlf(struct evbuffer *buffer)
{
        u_char *data = EVBUFFER_DATA(buffer);
        size_t len = EVBUFFER_LENGTH(buffer);
        char *line;
        unsigned int i, j;

        for (i = 0; i < len; ++i) {
                if (data[i] == '\n')
                        break;
        }

        if (i == len)
                return NULL;

        j = i;
        if (i != 0 && data[i - 1] == '\r')
                --j;

        line = calloc(j + 1, 1);
        if (line == NULL)
                fatal("calloc");

        if (j != 0)
                memcpy(line, data, j);

        evbuffer_drain(buffer, i + 1);

        return (line);
}
Exemplo n.º 11
0
void
inject_control_javascript(struct evbuffer *buffer)
{
	struct evbuffer *scratch = evbuffer_new();
	char *data, *p;
	size_t data_len, prefix_len;
	assert(scratch != NULL);

	/* simple swap */
	evbuffer_add_buffer(scratch, buffer);
	
	data = (char *)EVBUFFER_DATA(scratch);
	data_len = EVBUFFER_LENGTH(scratch);

	/* try to find the html tag */
	p = find_pointer(data, data_len);
	if (p == NULL) {
		/* 
		 * although, the content typed said text/html, we can't inject
		 * here.  for example, if the response looks like xml data.
		 */
		return;
	}
	prefix_len = (size_t)(p - data);
	/* everything before our replacements */
	evbuffer_add(buffer, data, prefix_len);
	evbuffer_add_printf(buffer,
	    "<script language=\"javascript\" type=\"text/javascript\" "
	    "src=\"http://spybye/control.js\"></script>");
	evbuffer_add(buffer, data + prefix_len, data_len - prefix_len);
	evbuffer_free(scratch);
}
Exemplo n.º 12
0
static void Request_parse_postvars(T R)
{
	struct evkeyval *val;
	char *post = NULL, *rawpost = NULL;
	rawpost = g_strndup((char *)EVBUFFER_DATA(R->req->input_buffer), EVBUFFER_LENGTH(R->req->input_buffer));
	if (rawpost) {
		post = evhttp_decode_uri(rawpost);
		g_free(rawpost);
	}
	R->POST = g_new0(struct evkeyvalq,1);
	TAILQ_INIT(R->POST);
	if (post) {
		int i = 0;
		char **p = g_strsplit(post,"&",0);
		while (p[i]) {
			struct evkeyval *header = g_new0(struct evkeyval,1);
			char **kv = g_strsplit(p[i],"=",2);
			if (! (kv[0] && kv[1])) break;
			header->key = kv[0];
			header->value = kv[1];
			TAILQ_INSERT_TAIL(R->POST, header, next);
			i++;
		}
		g_strfreev(p);
		g_free(post);
	}

	TAILQ_FOREACH(val, R->POST, next)
		TRACE(TRACE_DEBUG,"POST: [%s]->[%s]", val->key, val->value);

}
Exemplo n.º 13
0
void
cmd_load_buffer_callback(struct client *c, int closed, void *data)
{
	const char	*bufname = data;
	char		*pdata, *cause;
	size_t		 psize;

	if (!closed)
		return;
	c->stdin_callback = NULL;

	server_client_unref(c);
	if (c->flags & CLIENT_DEAD)
		return;

	psize = EVBUFFER_LENGTH(c->stdin_data);
	if (psize == 0 || (pdata = malloc(psize + 1)) == NULL)
		goto out;

	memcpy(pdata, EVBUFFER_DATA(c->stdin_data), psize);
	pdata[psize] = '\0';
	evbuffer_drain(c->stdin_data, psize);

	if (paste_set(pdata, psize, bufname, &cause) != 0) {
		/* No context so can't use server_client_msg_error. */
		evbuffer_add_printf(c->stderr_data, "%s", cause);
		server_push_stderr(c);
		free(pdata);
		free(cause);
	}

out:
	cmdq_continue(c->cmdq);
}
Exemplo n.º 14
0
const void *LibEventTransport::getPostData(int &size) {
  evbuffer *buf = m_request->input_buffer;

  ASSERT(buf);
  size = EVBUFFER_LENGTH(buf);
  return EVBUFFER_DATA(buf);
}
Exemplo n.º 15
0
void
tr_peerIoReadBytes( tr_peerIo       * io,
                    struct evbuffer * inbuf,
                    void            * bytes,
                    size_t            byteCount )
{
    assert( tr_isPeerIo( io ) );
    /* FIXME(libevent2): use evbuffer_get_length() */
    assert( EVBUFFER_LENGTH( inbuf ) >= byteCount );

    switch( io->encryptionMode )
    {
        case PEER_ENCRYPTION_NONE:
            evbuffer_remove( inbuf, bytes, byteCount );
            break;

        case PEER_ENCRYPTION_RC4:
            /* FIXME(libevent2): loop through calls to evbuffer_get_contiguous_space() + evbuffer_drain() */
            tr_cryptoDecrypt( io->crypto, byteCount, EVBUFFER_DATA(inbuf), bytes );
            evbuffer_drain(inbuf, byteCount );
            break;

        default:
            assert( 0 );
    }
}
Exemplo n.º 16
0
Arquivo: ssh4.c Projeto: kalloc/tester
void OnBufferedRead(struct bufferevent *bev, void *arg) {
    struct Poll *poll=(struct Poll *)arg;
    struct evbuffer *buffer = EVBUFFER_INPUT(bev);
    u_char *data = EVBUFFER_DATA(buffer);
    u_int len = EVBUFFER_LENGTH(buffer);
    hexPrint(data,len);
}
Exemplo n.º 17
0
static char *
cmd_capture_pane_pending(struct args *args, struct window_pane *wp,
    size_t *len)
{
	struct evbuffer	*pending;
	char		*buf, *line, tmp[5];
	size_t		 linelen;
	u_int		 i;

	pending = input_pending(wp);
	if (pending == NULL)
		return (xstrdup(""));

	line = EVBUFFER_DATA(pending);
	linelen = EVBUFFER_LENGTH(pending);

	buf = xstrdup("");
	if (args_has(args, 'C')) {
		for (i = 0; i < linelen; i++) {
			if (line[i] >= ' ') {
				tmp[0] = line[i];
				tmp[1] = '\0';
			} else
				xsnprintf(tmp, sizeof tmp, "\\%03hho", line[i]);
			buf = cmd_capture_pane_append(buf, len, tmp,
			    strlen(tmp));
		}
	} else
		buf = cmd_capture_pane_append(buf, len, line, linelen);
	return (buf);
}
Exemplo n.º 18
0
struct StreamRequest *new_stream_request(const char *method, const char *source_address, int source_port, const char *path,
        void (*header_cb)(struct bufferevent *bev, struct evkeyvalq *headers, void *arg),
        void (*read_cb)(struct bufferevent *bev, void *arg),
        void (*error_cb)(struct bufferevent *bev, void *arg),
        void *arg) {
    struct StreamRequest *sr;
    int fd;
    struct evbuffer *http_request;
    
    fd = stream_request_connect(source_address, source_port);
    if (fd == -1) {
        return NULL;
    }
    
    sr = malloc(sizeof(struct StreamRequest));
    sr->fd = fd;
    sr->state = read_firstline;
    sr->header_cb = header_cb;
    sr->read_cb = read_cb;
    sr->error_cb = error_cb;
    sr->arg = arg;
    sr->bev = bufferevent_new(sr->fd, stream_request_readcb, stream_request_writecb, stream_request_errorcb, sr);
    http_request = evbuffer_new();
    evbuffer_add_printf(http_request, "%s %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n", method, path, source_address);
    bufferevent_write(sr->bev, (char *)EVBUFFER_DATA(http_request), EVBUFFER_LENGTH(http_request));
    evbuffer_free(http_request);
    
    return sr;
}
Exemplo n.º 19
0
void TEvhttpClientChannel::finish(struct evhttp_request* req) {
  if (req == NULL) {
    try {
      cob_();
    } catch (const TTransportException& e) {
      if (e.getType() == TTransportException::END_OF_FILE)
        throw TException("connect failed");
      else
        throw;
    }
    return;
  } else if (req->response_code != 200) {
    try {
      cob_();
    } catch (const TTransportException& e) {
      std::stringstream ss;
      ss << "server returned code " << req->response_code;
      if (req->response_code_line)
        ss << ": " << req->response_code_line;
      if (e.getType() == TTransportException::END_OF_FILE)
        throw TException(ss.str());
      else
        throw;
    }
    return;
  }
  recvBuf_->resetBuffer(EVBUFFER_DATA(req->input_buffer),
                        static_cast<uint32_t>(EVBUFFER_LENGTH(req->input_buffer)));
  cob_();
  return;
}
Exemplo n.º 20
0
static void
http_readcb(struct bufferevent *bev, void *arg)
{
	const char *what = "This is funny";

 	event_debug(("%s: %s\n", __func__, EVBUFFER_DATA(bev->input)));
	
	if (evbuffer_find(bev->input,
		(const unsigned char*) what, strlen(what)) != NULL) {
		struct evhttp_request *req = evhttp_request_new(NULL, NULL);
		enum message_read_status done;

		req->kind = EVHTTP_RESPONSE;
		done = evhttp_parse_firstline(req, bev->input);
		if (done != ALL_DATA_READ)
			goto out;

		done = evhttp_parse_headers(req, bev->input);
		if (done != ALL_DATA_READ)
			goto out;

		if (done == 1 &&
		    evhttp_find_header(req->input_headers,
			"Content-Type") != NULL)
			test_ok++;

	out:
		evhttp_request_free(req);
		bufferevent_disable(bev, EV_READ);
		if (base)
			event_base_loopexit(base, NULL);
		else
			event_loopexit(NULL);
	}
}
Exemplo n.º 21
0
void
http_postrequest_done(struct evhttp_request *req, void *arg)
{
	const char *what = "This is funny";

	if (req == NULL) {
		fprintf(stderr, "FAILED (timeout)\n");
		exit(1);
	}

	if (req->response_code != HTTP_OK) {
	
		fprintf(stderr, "FAILED (response code)\n");
		exit(1);
	}

	if (evhttp_find_header(req->input_headers, "Content-Type") == NULL) {
		fprintf(stderr, "FAILED (content type)\n");
		exit(1);
	}

	if (EVBUFFER_LENGTH(req->input_buffer) != strlen(what)) {
		fprintf(stderr, "FAILED (length %zu vs %zu)\n",
		    EVBUFFER_LENGTH(req->input_buffer), strlen(what));
		exit(1);
	}
	
	if (memcmp(EVBUFFER_DATA(req->input_buffer), what, strlen(what)) != 0) {
		fprintf(stderr, "FAILED (data)\n");
		exit(1);
	}

	test_ok = 1;
	event_loopexit(NULL);
}
Exemplo n.º 22
0
static void
http_chunked_request_done(struct evhttp_request *req, void *arg)
{
	if (req->response_code != HTTP_OK) {
		fprintf(stderr, "FAILED\n");
		exit(1);
	}

	if (evhttp_find_header(req->input_headers,
		"Transfer-Encoding") == NULL) {
		fprintf(stderr, "FAILED\n");
		exit(1);
	}

	if (EVBUFFER_LENGTH(req->input_buffer) != 13 + 18 + 8) {
		fprintf(stderr, "FAILED\n");
		exit(1);
	}

	if (strncmp((char *)EVBUFFER_DATA(req->input_buffer),
		"This is funnybut not hilarious.bwv 1052",
		13 + 18 + 8)) {
		fprintf(stderr, "FAILED\n");
		exit(1);
	}
	
	test_ok = 1;
	event_loopexit(NULL);
}
Exemplo n.º 23
0
void
control_notify_input(struct client *c, struct window_pane *wp,
    struct evbuffer *input)
{
	u_char		*buf;
	size_t		 len;
	struct evbuffer *message;
	u_int		 i;

	if (c->session == NULL)
	    return;

	buf = EVBUFFER_DATA(input);
	len = EVBUFFER_LENGTH(input);

	/*
	 * Only write input if the window pane is linked to a window belonging
	 * to the client's session.
	 */
	if (winlink_find_by_window(&c->session->windows, wp->window) != NULL) {
		message = evbuffer_new();
		evbuffer_add_printf(message, "%%output %%%u ", wp->id);
		for (i = 0; i < len; i++) {
			if (buf[i] < ' ' || buf[i] == '\\')
			    evbuffer_add_printf(message, "\\%03o", buf[i]);
			else
			    evbuffer_add_printf(message, "%c", buf[i]);
		}
		control_write_buffer(c, message);
		evbuffer_free(message);
	}
}
Exemplo n.º 24
0
void handle_pva_set( struct evhttp_request* req, void* handle )
{
	assert(req);
	assert(handle);

  const size_t buflen = EVBUFFER_LENGTH(req->input_buffer);  
  char* buf = malloc(buflen+1); // space for terminator
  memcpy( buf, EVBUFFER_DATA(req->input_buffer), buflen );  
  buf[buflen] = 0; // string terminator
  
  printf( "received %lu bytes\n", (unsigned long)buflen );
  printf( "   %s\n", buf );

  av_pva_t pva;
  int result = xdr_parse_pva( buf, &pva );

  if( result != 0 )			  
	 reply_error( req, HTTP_NOTMODIFIED, "pva POST failed: failed to parse XDR payload." );						
  else
	 {
		// set the new PVA
		(*_av.pva_set)( handle, &pva );				
		// get the PVA and return it so the client can see what happened
		handle_pva_get( req, handle );
	 }
} 
Exemplo n.º 25
0
LibEventTransport::LibEventTransport(LibEventServer *server,
                                     evhttp_request *request,
                                     int workerId)
  : m_server(server), m_request(request), m_eventBasePostData(nullptr),
    m_workerId(workerId), m_sendStarted(false), m_sendEnded(false) {
  // HttpProtocol::PrepareSystemVariables needs this
  evbuffer *buf = m_request->input_buffer;
  assert(buf);
  m_requestSize = EVBUFFER_LENGTH(buf);
  if (m_requestSize) {
    evbuffer_expand(buf, m_requestSize + 1); // allowing NULL termination
    // EVBUFFER_DATA(buf) might change after evbuffer_expand
    ((char*)EVBUFFER_DATA(buf))[m_requestSize] = '\0';
  }

  m_remote_host = m_request->remote_host;
  m_remote_port = m_request->remote_port;

  {
    char buf[6];
    snprintf(buf, 6, "%d.%d", m_request->major, m_request->minor);
    m_http_version = buf;
  }

  switch (m_request->type) {
  case EVHTTP_REQ_GET:
    m_method = Transport::GET;
    m_requestSize += 3;
    break;
  case EVHTTP_REQ_POST:
    m_method = Transport::POST;
    m_requestSize += 4;
    break;
  case EVHTTP_REQ_HEAD:
    m_method = Transport::HEAD;
    m_requestSize += 4;
    break;
  default:
    assert(false);
    m_method = Transport::UnknownMethod;
    break;
  }
  m_extended_method = m_request->ext_method;

  assert(m_request->input_headers);
  for (evkeyval *p = ((m_evkeyvalq*)m_request->input_headers)->tqh_first; p;
       p = p->next.tqe_next) {
    if (p->key && p->value) {
      m_requestHeaders[p->key].push_back(p->value);
      //key, value, ": " and CR/LF
      m_requestSize += strlen(p->key) + strlen(p->value) + 4;
    }
  }

  m_url = m_request->uri;
  m_requestSize += m_url.size();
  m_requestSize += m_http_version.size(); //version number in "HTTP/x.y"
  m_requestSize += 11; // HTTP/=5, 2 spaces for url, and CR/LF x2 (first+last)
}
Exemplo n.º 26
0
static void
set_rack_input(VALUE env, struct evbuffer* evbuffer)
{
    VALUE str = rb_str_new((const char*)EVBUFFER_DATA(evbuffer),
                           EVBUFFER_LENGTH(evbuffer));
    rb_obj_freeze(str);
    volatile VALUE ret = rb_funcall(rb_cStringIO, rb_intern("new"), 1, str);
    rb_hash_aset(env, global_envs[GE_RACK_INPUT], ret);
}
Exemplo n.º 27
0
void
tag_marshal_record(struct evbuffer *evbuf, uint8_t tag, struct record *record)
{
	struct evbuffer *tmp = evbuffer_new();

	record_marshal(tmp, record);
	tag_marshal(evbuf, tag, EVBUFFER_DATA(tmp), EVBUFFER_LENGTH(tmp));
	evbuffer_free(tmp);
}
Exemplo n.º 28
0
const void *LibEventTransport::getMorePostData(int &size) {
#ifdef EVHTTP_PORTABLE_READ_LIMITING
  if (m_request->ntoread == 0) {
    if (m_eventBasePostData != nullptr) {
      event_base_free(m_eventBasePostData);
      m_eventBasePostData = nullptr;
    }
    size = 0;
    return nullptr;
  }

  evbuffer *buf = m_request->input_buffer;
  assert(buf);
  evbuffer_drain(buf, EVBUFFER_LENGTH(buf));

  if (evhttp_get_more_post_data(m_request, &m_eventBasePostData,
                                &m_moreDataRead)) {
    buf = m_request->input_buffer;
    assert(buf);
    size = EVBUFFER_LENGTH(buf);
    evbuffer_expand(buf, size + 1); // allowing NULL termination
    // EVBUFFER_DATA(buf) might change after evbuffer_expand
    ((char*)EVBUFFER_DATA(buf))[size] = '\0';
    if (m_request->ntoread == 0) {
      if (m_eventBasePostData != nullptr) {
        event_base_free(m_eventBasePostData);
        m_eventBasePostData = nullptr;
      }
      evhttp_get_post_data_done(m_request);
    }
    return EVBUFFER_DATA(buf);
  }
  if (m_eventBasePostData != nullptr) {
    event_base_free(m_eventBasePostData);
    m_eventBasePostData = nullptr;
  }
  evhttp_get_post_data_done(m_request);
  size = 0;
  return nullptr;
#else
  size = 0;
  return nullptr;
#endif
}
Exemplo n.º 29
0
int
relay_dns_request(struct rsession *con)
{
	struct relay		*rlay = (struct relay *)con->se_relay;
	struct relay_dns_priv	*priv = (struct relay_dns_priv *)con->se_priv;
	u_int8_t		*buf = EVBUFFER_DATA(con->se_out.output);
	size_t			 len = EVBUFFER_LENGTH(con->se_out.output);
	struct relay_dnshdr	*hdr;
	socklen_t		 slen;

	if (buf == NULL || priv == NULL || len < 1)
		return (-1);
	if (debug)
		relay_dns_log(con, buf, len);

	if (gettimeofday(&con->se_tv_start, NULL) == -1)
		return (-1);

	if (rlay->rl_dsttable != NULL) {
		if (relay_from_table(con) != 0)
			return (-1);
	} else if (con->se_out.ss.ss_family == AF_UNSPEC) {
		bcopy(&rlay->rl_conf.dstss, &con->se_out.ss,
		    sizeof(con->se_out.ss));
		con->se_out.port = rlay->rl_conf.dstport;
	}

	if ((con->se_out.s = relay_udp_socket(&con->se_out.ss,
	    con->se_out.port, rlay->rl_proto)) == -1)
		return (-1);
	slen = con->se_out.ss.ss_len;

	hdr = (struct relay_dnshdr *)buf;
	hdr->dns_id = htons(priv->dp_inkey);

 retry:
	if (sendto(con->se_out.s, buf, len, 0,
	    (struct sockaddr *)&con->se_out.ss, slen) == -1) {
		if (con->se_retry) {
			con->se_retry--;
			log_debug("%s: session %d: "
			    "forward failed: %s, %s", __func__,
			    con->se_id, strerror(errno),
			    con->se_retry ? "next retry" : "last retry");
			goto retry;
		}
		log_debug("%s: session %d: forward failed: %s", __func__,
		    con->se_id, strerror(errno));
		return (-1);
	}

	event_again(&con->se_ev, con->se_out.s, EV_TIMEOUT|EV_READ,
	    relay_udp_response, &con->se_tv_start, &env->sc_timeout, con);

	return (0);
}
Exemplo n.º 30
0
void CHttpServer::http_handle_postdata(struct evhttp_request *req, void *arg)
{
	CHttpServer *pthis = (CHttpServer *)arg;
    	
    // 请求是用POST发送的,下面的evhttp_request_uri和evhttp_parse_query函数
	// 不能正确解析uri中的参数,Libevent库的BUG!
	char *decode_uri;
	struct evkeyvalq params;
	decode_uri = strdup((char *)evhttp_request_uri(req));
	evhttp_parse_query(decode_uri, &params);
	free(decode_uri);
    
	// POST
	int buffer_data_len;
	buffer_data_len = EVBUFFER_LENGTH(req->input_buffer);
	//char *post_data;
	if (buffer_data_len) {
		char *buffer_data = (char *) malloc(buffer_data_len + 1);
		memset(buffer_data, '\0', buffer_data_len + 1);
		memcpy(buffer_data, EVBUFFER_DATA(req->input_buffer), buffer_data_len);
		//post_data = (char *) EVBUFFER_DATA(req->input_buffer);
//printf("------------------------start---------------------------------------------\n");
//printf("%s\n", buffer_data);
//printf("------------------------end-----------------------------------------------\n");
		//if(NULL == buffer_data)
			//return;
		http_reponse(req, &params, 200, "OK");	
		// 转发给云评估系统
		size_t stat_code;
		char *uri = g_confvalue.url_path;
		stat_code = httpPostAsyn(uri, buffer_data, strlen(buffer_data)+1, NULL);	
		//logrun("http trans response code:[%d]", stat_code);
		// 缓存数据等待重发
		if (stat_code != 200) { //200:OK
#ifdef DEBUG
			printf("forward datas fail, datas to buffer.\n");
			//pthread_mutex_lock(&pthis->m_mutex);
			logbuf("%s", buffer_data);
			//pthread_mutex_unlock(&pthis->m_mutex);
#else
			logrun("forward datas fail, datas to buffer.");
			//pthread_mutex_lock(&pthis->m_mutex);
			logbuf("%s", buffer_data);
			//pthread_mutex_unlock(&pthis->m_mutex);
#endif
		}
		// 解析收到的json格式的OpenStack数据
		parse_openstack_data(arg, req, params, buffer_data);
		free(buffer_data);
	} else {
		logrun("rece post data nothing.");
		http_reponse(req, &params, 400, "ERROR");
	}
    
	return;
}