static int parse_response_status_code(struct evbuffer * evb){ char * line = evbuffer_readline(evb); int code; sscanf(line, "HTTP/%*d.%*d %d", & code); free(line); return code; }
void bufread(struct bufferevent *bev, void *data) { char *line; char *x = (char *)data; while ( (line = evbuffer_readline(bev->input)) != NULL ) { printf("%s: '%s'\n", x, line); free(line); } }
char *redsocks_evbuffer_readline(struct evbuffer *buf) { #if _EVENT_NUMERIC_VERSION >= 0x02000000 return evbuffer_readln(buf, NULL, EVBUFFER_EOL_CRLF); #else return evbuffer_readline(buf); #endif }
void test_get() { char* p; //http://www.baidu.com/search/error.html http_response * response = http_get("220.181.111.85", 80, "/"); // xiaonei.com/home printf("%d\n", response -> status_code); while(( p = evbuffer_readline(response->headers))) { printf("%s\n", p); free(p); } while(( p = evbuffer_readline(response->body))) { printf("%s\n", p); free(p); } http_response_free(response); }
void test_post() { char* p; http_response * response = http_post("123.125.44.242", 80, "/Plogin.do", "[email protected]&password=88888"); // xiaonei.com/home printf("%d\n", response -> status_code); while(( p = evbuffer_readline(response->headers))) { printf("%s\n", p); free(p); } while(( p = evbuffer_readline(response->body))) { printf("%s\n", p); free(p); } http_response_free(response); }
void _program_file_read_buffer(struct bufferevent *bev, void *data) { grok_input_t *ginput = (grok_input_t *)data; grok_program_t *gprog = ginput->gprog; char *line; int i; while ((line = evbuffer_readline(EVBUFFER_INPUT(bev))) != NULL) { grok_matchconfig_exec(gprog, ginput, line); free(line); } }
void cmd_run_shell_callback(struct job *job) { struct cmd_run_shell_data *cdata = job->data; struct cmd_ctx *ctx = &cdata->ctx; char *cmd, *msg, *line; size_t size; int retcode; u_int lines; if (ctx->cmdclient != NULL && ctx->cmdclient->flags & CLIENT_DEAD) return; if (ctx->curclient != NULL && ctx->curclient->flags & CLIENT_DEAD) return; lines = 0; do { if ((line = evbuffer_readline(job->event->input)) != NULL) { ctx->print(ctx, "%s", line); lines++; } } while (line != NULL); size = EVBUFFER_LENGTH(job->event->input); if (size != 0) { line = xmalloc(size + 1); memcpy(line, EVBUFFER_DATA(job->event->input), size); line[size] = '\0'; ctx->print(ctx, "%s", line); lines++; xfree(line); } cmd = cdata->cmd; msg = NULL; if (WIFEXITED(job->status)) { if ((retcode = WEXITSTATUS(job->status)) != 0) xasprintf(&msg, "'%s' returned %d", cmd, retcode); } else if (WIFSIGNALED(job->status)) { retcode = WTERMSIG(job->status); xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode); } if (msg != NULL) { if (lines != 0) ctx->print(ctx, "%s", msg); else ctx->info(ctx, "%s", msg); xfree(msg); } }
void chldreadcb(struct bufferevent *b, void *arg) { char *line, *sp, *ap; int n, i, total, nprocs = *(int *)arg; if((line=evbuffer_readline(b->input)) != nil){ sp = line; if((ap = strsep(&sp, "\t")) == nil) panic("report error\n"); n = atoi(ap); if(n - nreport > NBUFFER) panic("a process fell too far behind\n"); n %= NBUFFER; for(i=0; i<params.nbuckets + 3 && (ap=strsep(&sp, "\t")) != nil; i++) reportbuf[n][i] += atoi(ap); if(++nreportbuf[n] >= nprocs){ /* Timestamp it. */ printf("%d\t",(int)time(nil)); for(i = 0; i < params.nbuckets + 3; i++) printf("%d\t", reportbuf[n][i]); /* Compute the total rate of succesful requests. */ total = 0; for(i=3; i<params.nbuckets+1; i++) total += reportbuf[n][i]; printf("%d\n", mkrate(&lastreporttv, total)); /* Aggregate. */ counts.errors += reportbuf[n][0]; counts.timeouts += reportbuf[n][1]; counts.closes += reportbuf[n][2]; for(i=0; i<params.nbuckets; i++){ counts.successes += reportbuf[n][i + 3]; counts.counters[i] += reportbuf[n][i + 3]; } /* Clear it. Advance nreport. */ memset(reportbuf[n], 0,(params.nbuckets + 3) * sizeof(int)); nreportbuf[n] = 0; nreport++; } free(line); } bufferevent_enable(b, EV_READ); }
void cmd_run_shell_callback(struct job *job) { struct cmd_run_shell_data *cdata = job->data; struct cmd_q *cmdq = cdata->cmdq; char *cmd, *msg, *line; size_t size; int retcode; u_int lines; if (cmdq->dead) return; cmd = cdata->cmd; lines = 0; do { if ((line = evbuffer_readline(job->event->input)) != NULL) { cmd_run_shell_print(job, line); free(line); lines++; } } while (line != NULL); size = EVBUFFER_LENGTH(job->event->input); if (size != 0) { line = xmalloc(size + 1); memcpy(line, EVBUFFER_DATA(job->event->input), size); line[size] = '\0'; cmd_run_shell_print(job, line); lines++; free(line); } msg = NULL; if (WIFEXITED(job->status)) { if ((retcode = WEXITSTATUS(job->status)) != 0) xasprintf(&msg, "'%s' returned %d", cmd, retcode); } else if (WIFSIGNALED(job->status)) { retcode = WTERMSIG(job->status); xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode); } if (msg != NULL) { if (lines == 0) cmdq_info(cmdq, "%s", msg); else cmd_run_shell_print(job, msg); free(msg); } }
static char *get_auth_request_header(struct evbuffer *buf) { char *line; for (;;) { line = evbuffer_readline(buf); if (line == NULL || *line == '\0' || strchr(line, ':') == NULL) { free(line); return NULL; } if (strncasecmp(line, auth_request_header, strlen(auth_request_header)) == 0) return line; free(line); } }
void chldreadcb(struct bufferevent *b, void *arg) { char *line, *sp, *ap; int n, i, nprocs = *(int *)arg; if((line=evbuffer_readline(b->input)) != nil){ sp = line; if((ap = strsep(&sp, "\t")) == nil) panic("report error\n"); n = atoi(ap); if(n - nreport > NBUFFER) panic("a process fell too far behind\n"); n %= NBUFFER; for(i=0; i<params.nbuckets + num_cols && (ap=strsep(&sp, "\t")) != nil; i++) reportbuf[n][i] += atoi(ap); if(++nreportbuf[n] >= nprocs){ /* Timestamp it. */ printf("%d\t",(int)time(nil)); for(i = 0; i < params.nbuckets + num_cols; i++) printf("%d\t", reportbuf[n][i]); printf("%ld\n", mkrate(&lastreporttv, reportbuf[n][0])); reset_time(&lastreporttv); /* Aggregate. */ counts.conn_successes += reportbuf[n][0]; counts.conn_errors += reportbuf[n][1]; counts.conn_timeouts += reportbuf[n][2]; counts.conn_closes += reportbuf[n][3]; counts.http_successes += reportbuf[n][4]; counts.http_errors += reportbuf[n][5]; for(i=0; i<params.nbuckets; i++) counts.counters[i] += reportbuf[n][i + num_cols]; /* Clear it. Advance nreport. */ memset(reportbuf[n], 0,(params.nbuckets + num_cols) * sizeof(int)); nreportbuf[n] = 0; nreport++; } free(line); } bufferevent_enable(b, EV_READ); }
void buf_read_callback(struct bufferevent *incoming, void *arg) { struct evbuffer *evreturn; char *req; req = evbuffer_readline(incoming->input); if (req == NULL) return; evreturn = evbuffer_new(); evbuffer_add_printf(evreturn, "You said %s\n", req); bufferevent_write_buffer(incoming, evreturn); evbuffer_free(evreturn); free(req); }
static void network_read_callback(struct bufferevent *bufev, void *arg) { char *line; while (1) { line = evbuffer_readline(EVBUFFER_INPUT(bufev)); if ( line == NULL ) break; if ( g_data.readline_cb ) g_data.readline_cb((int)bufev, line); free(line); } }
static void server_readable(int fd, short event, void *arg) { client_t *client = (client_t*)arg; // Der Client wurde 'extern' gekickt, allerdings noch // nicht entfernt. Dann wird dieser Readcallback aufgerufen, // sollte allerdings nichts mehr machen. if (client->kill_me) return; int ret = evbuffer_read(client->in_buf, fd, 128); if (ret < 0) { server_destroy(client, strerror(errno)); } else if (ret == 0) { server_destroy(client, "eof reached"); } else if (EVBUFFER_LENGTH(client->in_buf) > 8192) { server_destroy(client, "line too long. go away."); } else { char *line; while ((line = evbuffer_readline(client->in_buf))) { lua_pushliteral(L, "on_client_input"); lua_rawget(L, LUA_GLOBALSINDEX); lua_pushnumber(L, client_num(client)); lua_pushstring(L, line); free(line); // Cycles fuer die Verarbeitung hochsetzen lua_set_cycles(L, 0xFFFFFF); // Input verarbeiten output_client = client; if (lua_pcall(L, 2, 0, 0) != 0) { fprintf(stderr, "error calling on_client_input: %s\n", lua_tostring(L, -1)); server_writeto(client, lua_tostring(L, -1), lua_strlen(L, -1)); lua_pop(L, 1); } output_client = NULL; // Kill Me Flag waehrend Aufruf von on_client_input // gesetzt? Direkt rausschmeissen! if (client->kill_me) { server_destroy(client, client->kill_me); return; } } } }
static void cmd_run_shell_callback(struct job *job) { struct cmd_run_shell_data *cdata = job->data; char *cmd = cdata->cmd, *msg, *line; size_t size; int retcode; do { if ((line = evbuffer_readline(job->event->input)) != NULL) { cmd_run_shell_print(job, line); free(line); } } while (line != NULL); size = EVBUFFER_LENGTH(job->event->input); if (size != 0) { line = xmalloc(size + 1); memcpy(line, EVBUFFER_DATA(job->event->input), size); line[size] = '\0'; cmd_run_shell_print(job, line); free(line); } msg = NULL; if (WIFEXITED(job->status)) { if ((retcode = WEXITSTATUS(job->status)) != 0) xasprintf(&msg, "'%s' returned %d", cmd, retcode); } else if (WIFSIGNALED(job->status)) { retcode = WTERMSIG(job->status); xasprintf(&msg, "'%s' terminated by signal %d", cmd, retcode); } if (msg != NULL) cmd_run_shell_print(job, msg); free(msg); if (cdata->item != NULL) cdata->item->flags &= ~CMDQ_WAITING; }
int main(int argc, char** argv) { struct evbuffer* buff = NULL; char c, c2[3] = {0}; buff = evbuffer_new(); assert(buff != NULL); evbuffer_add(buff, "1", 1); evbuffer_add(buff, "2", 1); evbuffer_add(buff, "3", 1); evbuffer_add_printf(buff, "%d%d", 4, 5); assert(buff->off == 5); evbuffer_remove(buff, &c, sizeof(char)); assert(c == '1'); evbuffer_remove(buff, &c, sizeof(char)); assert(c == '2'); evbuffer_remove(buff, &c, sizeof(char)); assert(c == '3'); evbuffer_remove(buff, c2, 2); assert(strcmp(c2, "45") == 0); assert(buff->off == 0); evbuffer_add(buff, "test\r\n", 6); assert(buff->off == 6); char* line = evbuffer_readline(buff); assert(strcmp(line, "test") ==0); assert(buff->off == 0); free(line); evbuffer_free(buff); printf("ok\n"); return 0; }
static void http_chunked_errorcb(struct bufferevent *bev, short what, void *arg) { if (!test_ok) goto out; test_ok = -1; if ((what & EVBUFFER_EOF) != 0) { struct evhttp_request *req = evhttp_request_new(NULL, NULL); const char *header; enum message_read_status done; req->kind = EVHTTP_RESPONSE; done = evhttp_parse_firstline(req, EVBUFFER_INPUT(bev)); if (done != ALL_DATA_READ) goto out; done = evhttp_parse_headers(req, EVBUFFER_INPUT(bev)); if (done != ALL_DATA_READ) goto out; header = evhttp_find_header(req->input_headers, "Transfer-Encoding"); if (header == NULL || strcmp(header, "chunked")) goto out; header = evhttp_find_header(req->input_headers, "Connection"); if (header == NULL || strcmp(header, "close")) goto out; header = evbuffer_readline(EVBUFFER_INPUT(bev)); if (header == NULL) goto out; /* 13 chars */ if (strcmp(header, "d")) goto out; free((char*)header); if (strncmp((char *)EVBUFFER_DATA(EVBUFFER_INPUT(bev)), "This is funny", 13)) goto out; evbuffer_drain(EVBUFFER_INPUT(bev), 13 + 2); header = evbuffer_readline(EVBUFFER_INPUT(bev)); if (header == NULL) goto out; /* 18 chars */ if (strcmp(header, "12")) goto out; free((char *)header); if (strncmp((char *)EVBUFFER_DATA(EVBUFFER_INPUT(bev)), "but not hilarious.", 18)) goto out; evbuffer_drain(EVBUFFER_INPUT(bev), 18 + 2); header = evbuffer_readline(EVBUFFER_INPUT(bev)); if (header == NULL) goto out; /* 8 chars */ if (strcmp(header, "8")) goto out; free((char *)header); if (strncmp((char *)EVBUFFER_DATA(EVBUFFER_INPUT(bev)), "bwv 1052.", 8)) goto out; evbuffer_drain(EVBUFFER_INPUT(bev), 8 + 2); header = evbuffer_readline(EVBUFFER_INPUT(bev)); if (header == NULL) goto out; /* 0 chars */ if (strcmp(header, "0")) goto out; free((char *)header); test_ok = 2; } out: event_loopexit(NULL); }
//void drive_machine(conn* c){ //改成bufferevent之后event_handler和drive_machine都不能幸免需要改参数 void drive_machine(conn* c, struct bufferevent * incoming){ assert(c != NULL); // 真正的处理函数,根据不同的conn的状态来进行不同的处理。 // 虽然我做这个demo只是把消息回显而已,但貌似这么多种状态还是得处理。 bool stop = false; int sfd; socklen_t addrlen; struct sockaddr_storage addr; struct evbuffer *evreturn; char *req; while(!stop){ switch(c->state){ case conn_listening: //如果是listenting状态,则需要把连接accept addrlen = sizeof(addr); sfd = accept(c->sfd, (struct sockaddr *)&addr, &addrlen); if (sfd == -1) { // accept错误 perror("sfd accept failed"); accept_new_conns(false); //用来决定主线程是否还需要accept连接,如果已经接近饱和了,就停住,等一个时间间隔再来试。 stop = true; } //分派conn任务 dispatch_conn_new(sfd, conn_new_cmd, EV_READ | EV_PERSIST, DATA_BUFFER_SIZE, tcp_transport); stop = true; break; case conn_new_cmd: /* fall through */ case conn_read: /* now try reading from the socket */ req = evbuffer_readline(incoming->input); if (req == NULL){ //conn_set_state(c, conn_closing); //goto set_conn_closing; conn_set_state(c, conn_waiting); break; } if(c->req != NULL){ free(c->req); c->req = NULL; } c->req = req; conn_set_state(c, conn_mwrite); //if (!update_event(c, EV_WRITE | EV_PERSIST)){ // printf("conn_read update event failed"); // goto set_conn_closing; //} //stop = true; //stop = true; break; //evreturn = evbuffer_new(); //evbuffer_add_printf(evreturn, "You said %s\n", req); // bufferevent_write_buffer(incoming, evreturn); //evbuffer_free(evreturn); //free(req); set_conn_closing: conn_set_state(c, conn_closing); break; case conn_mwrite: //所有的回复到在这个函数进行输出 req = c->req; evreturn = evbuffer_new(); // bufferevent_setwatermark(incoming, EV_WRITE, 0, 0); int res1 = evbuffer_add_printf(evreturn, "You said %s\n", req); int res2 = bufferevent_write_buffer(incoming, evreturn); // int res3 = bufferevent_flush(incoming, EV_WRITE, BEV_FLUSH); int res4 = evbuffer_get_length(incoming->output); evbuffer_free(evreturn); free(req); evreturn = NULL; //int fd = (int)bufferevent_getfd(incoming); //char buf[20]; // //sprintf(buf, "You said %s\n", req); //write(fd, buf, 11); c->req = NULL; conn_set_state(c,conn_waiting); stop = true; break; case conn_waiting: if (!update_event(c, EV_READ | EV_PERSIST)) { fprintf(stderr, "Couldn't update event\n"); conn_set_state(c, conn_closing); break; } conn_set_state(c, conn_read); stop = true; break; case conn_closing: conn_close(c); stop = true; break; } /* otherwise we have a real error, on which we close the connection */ } }
static void httpc_read_cb(struct bufferevent *buffev, void *_arg) { redsocks_client *client = _arg; int dropped = 0; assert(client->state >= httpc_request_sent); redsocks_touch_client(client); if (client->state == httpc_request_sent) { size_t len = EVBUFFER_LENGTH(buffev->input); char *line = evbuffer_readline(buffev->input); if (line) { unsigned int code; if (sscanf(line, "HTTP/%*u.%*u %u", &code) == 1) { // 1 == one _assigned_ match if (code == 407) { // auth failed http_auth *auth = (void*)(client->instance + 1); if (auth->last_auth_query != NULL && auth->last_auth_count == 1) { redsocks_log_error(client, LOG_NOTICE, "proxy auth failed"); redsocks_drop_client(client); dropped = 1; } else if (client->instance->config.login == NULL || client->instance->config.password == NULL) { redsocks_log_error(client, LOG_NOTICE, "proxy auth required, but no login information provided"); redsocks_drop_client(client); dropped = 1; } else { char *auth_request = get_auth_request_header(buffev->input); if (!auth_request) { redsocks_log_error(client, LOG_NOTICE, "403 found, but no proxy auth challenge"); redsocks_drop_client(client); dropped = 1; } else { free(line); free(auth->last_auth_query); char *ptr = auth_request; ptr += strlen(auth_request_header); while (isspace(*ptr)) ptr++; auth->last_auth_query = calloc(strlen(ptr) + 1, 1); strcpy(auth->last_auth_query, ptr); auth->last_auth_count = 0; free(auth_request); if (bufferevent_disable(client->relay, EV_WRITE)) { redsocks_log_errno(client, LOG_ERR, "bufferevent_disable"); return; } /* close relay tunnel */ close(EVENT_FD(&client->relay->ev_write)); bufferevent_free(client->relay); /* set to initial state*/ client->state = httpc_new; /* and reconnect */ redsocks_connect_relay(client); return; } } } else if (200 <= code && code <= 299) { client->state = httpc_reply_came; } else { redsocks_log_error(client, LOG_NOTICE, "%s", line); redsocks_drop_client(client); dropped = 1; } } free(line); } else if (len >= HTTP_HEAD_WM_HIGH) { redsocks_drop_client(client); dropped = 1; } } if (dropped) return; while (client->state == httpc_reply_came) { char *line = evbuffer_readline(buffev->input); if (line) { if (strlen(line) == 0) { client->state = httpc_headers_skipped; } free(line); } else { break; } } if (client->state == httpc_headers_skipped) { redsocks_start_relay(client); } }