static int check_header(const struct strbuf *line, struct strbuf *hdr_data[], int overwrite) { int i, ret = 0, len; struct strbuf sb = STRBUF_INIT; /* search for the interesting parts */ for (i = 0; header[i]; i++) { int len = strlen(header[i]); if ((!hdr_data[i] || overwrite) && cmp_header(line, header[i])) { /* Unwrap inline B and Q encoding, and optionally * normalize the meta information to utf8. */ strbuf_add(&sb, line->buf + len + 2, line->len - len - 2); decode_header(&sb); handle_header(&hdr_data[i], &sb); ret = 1; goto check_header_out; } } /* Content stuff */ if (cmp_header(line, "Content-Type")) { len = strlen("Content-Type: "); strbuf_add(&sb, line->buf + len, line->len - len); decode_header(&sb); strbuf_insert(&sb, 0, "Content-Type: ", len); handle_content_type(&sb); ret = 1; goto check_header_out; } if (cmp_header(line, "Content-Transfer-Encoding")) { len = strlen("Content-Transfer-Encoding: "); strbuf_add(&sb, line->buf + len, line->len - len); decode_header(&sb); handle_content_transfer_encoding(&sb); ret = 1; goto check_header_out; } /* for inbody stuff */ if (!prefixcmp(line->buf, ">From") && isspace(line->buf[5])) { ret = 1; /* Should this return 0? */ goto check_header_out; } if (!prefixcmp(line->buf, "[PATCH]") && isspace(line->buf[7])) { for (i = 0; header[i]; i++) { if (!memcmp("Subject", header[i], 7)) { handle_header(&hdr_data[i], line); ret = 1; goto check_header_out; } } } check_header_out: strbuf_release(&sb); return ret; }
static int check_header(char *line, unsigned linesize, char **hdr_data, int overwrite) { int i; /* search for the interesting parts */ for (i = 0; header[i]; i++) { int len = strlen(header[i]); if ((!hdr_data[i] || overwrite) && !strncasecmp(line, header[i], len) && line[len] == ':' && isspace(line[len + 1])) { /* Unwrap inline B and Q encoding, and optionally * normalize the meta information to utf8. */ decode_header(line + len + 2, linesize - len - 2); hdr_data[i] = xmalloc(1000 * sizeof(char)); if (! handle_header(line, hdr_data[i], len + 2)) { return 1; } } } /* Content stuff */ if (!strncasecmp(line, "Content-Type", 12) && line[12] == ':' && isspace(line[12 + 1])) { decode_header(line + 12 + 2, linesize - 12 - 2); if (! handle_content_type(line)) { return 1; } } if (!strncasecmp(line, "Content-Transfer-Encoding", 25) && line[25] == ':' && isspace(line[25 + 1])) { decode_header(line + 25 + 2, linesize - 25 - 2); if (! handle_content_transfer_encoding(line)) { return 1; } } /* for inbody stuff */ if (!memcmp(">From", line, 5) && isspace(line[5])) return 1; if (!memcmp("[PATCH]", line, 7) && isspace(line[7])) { for (i = 0; header[i]; i++) { if (!memcmp("Subject: ", header[i], 9)) { if (! handle_header(line, hdr_data[i], 0)) { return 1; } } } } /* no match */ return 0; }
bool ContextGroupBy::parseCmdArgs(int argc, char **argv, int skipFirstArgs) { for (_i=_skipFirstArgs; _i < argc; _i++) { if (isUsed(_i - _skipFirstArgs)) { continue; } else if ((strcmp(_argv[_i], "-g") == 0) || (strcmp(_argv[_i], "-grp") == 0)) { if (!handle_g()) return false; } else if (strcmp(_argv[_i], "-inheader") == 0) { if (!handle_inheader()) return false; } else if (strcmp(_argv[_i], "-outheader") == 0) { if (!handle_outheader()) return false; } else if (strcmp(_argv[_i], "-header") == 0) { if (!handle_header()) return false; } else if (strcmp(_argv[_i], "-full") == 0) { if (!handle_full()) return false; } else if (strcmp(_argv[_i], "-ignorecase") == 0) { if (!handle_ignorecase()) return false; } } return ContextBase::parseCmdArgs(argc, argv, _skipFirstArgs); }
bool ContextBase::parseCmdArgs(int argc, char **argv, int skipFirstArgs) { _argc = argc; _argv = argv; _skipFirstArgs = skipFirstArgs; if (_argc < 2) { setShowHelp(true); return false; } setProgram(_programNames[argv[0]]); _argsProcessed.resize(_argc - _skipFirstArgs, false); for (_i=_skipFirstArgs; _i < argc; _i++) { if (isUsed(_i - _skipFirstArgs)) { continue; } if (strcmp(_argv[_i], "-i") == 0) { if (!handle_i()) return false; } else if (strcmp(_argv[_i], "-g") == 0) { if (!handle_g()) return false; } else if ((strcmp(_argv[_i], "-h") == 0) || (strcmp(_argv[_i], "--help") == 0)) { if (!handle_h()) return false; } else if (strcmp(_argv[_i], "-split") == 0) { if (!handle_split()) return false; } else if (strcmp(_argv[_i], "-bed") == 0) { if (!handle_bed()) return false; } else if (strcmp(_argv[_i], "-ubam") == 0) { if (!handle_ubam()) return false; } else if (strcmp(_argv[_i], "-fbam") == 0) { if (!handle_fbam()) return false; } else if(strcmp(_argv[_i], "-sorted") == 0) { if (!handle_sorted()) return false; } else if (strcmp(_argv[_i], "-nobuf") == 0) { if (!handle_nobuf()) return false; } else if (strcmp(_argv[_i], "-header") == 0) { if (!handle_header()) return false; } else if (strcmp(_argv[_i], "-n") == 0) { if (!handle_n()) return false; } else if (strcmp(_argv[_i], "-seed") == 0) { if (!handle_seed()) return false; } } return true; }
/* Handle input by separating into lines and calling handle_header for each */ static int handle_buffer(conn_t* conn, char* buffer, int buf_size) { int i; char* lines[MAX_HEADERS]; char* body; char first_line[MAX_HEADER_SIZE]; switch (conn->state) { case READING_HEADERS: /* Set up lines[0] with the remnant from the previous read */ strncpy(first_line, conn->local_buffer, sizeof(first_line)); lines[0] = first_line; /* Split into lines (saving fragment in conn->local_buffer) */ body = get_lines(conn, lines, buffer, buf_size); for (i = 0; i < MAX_HEADERS; i++) handle_header(conn, lines[i]); if (body == NULL) /* Done for now; we're still reading headers */ return ST_CONTINUE; /* Otherwise, body points to the body of the reply */ if (verbose > 1) fputs("End Of Headers\n", stderr); conn->state = RECEIVING_DATA; buf_size = body - buffer; buffer = body; /* FALLTHROUGH */ case RECEIVING_DATA: /* * In the future, we'll actually be testing the returned * object to make sure the data is correct. (And the code * needs to handle the "chunked" transfer encoding.) For now, * we just discard it. */ if (verbose > 3) fprintf(stderr, "%s", buffer); break; default: fprintf(stderr, "Fatal error -- bad state %s\n", state_names[conn->state]); break; } return ST_CONTINUE; }
static int check_header(struct mailinfo *mi, const struct strbuf *line, struct strbuf *hdr_data[], int overwrite) { int i, ret = 0, len; struct strbuf sb = STRBUF_INIT; /* search for the interesting parts */ for (i = 0; header[i]; i++) { int len = strlen(header[i]); if ((!hdr_data[i] || overwrite) && cmp_header(line, header[i])) { /* Unwrap inline B and Q encoding, and optionally * normalize the meta information to utf8. */ strbuf_add(&sb, line->buf + len + 2, line->len - len - 2); decode_header(mi, &sb); handle_header(&hdr_data[i], &sb); ret = 1; goto check_header_out; } } /* Content stuff */ if (cmp_header(line, "Content-Type")) { len = strlen("Content-Type: "); strbuf_add(&sb, line->buf + len, line->len - len); decode_header(mi, &sb); strbuf_insert(&sb, 0, "Content-Type: ", len); handle_content_type(mi, &sb); ret = 1; goto check_header_out; } if (cmp_header(line, "Content-Transfer-Encoding")) { len = strlen("Content-Transfer-Encoding: "); strbuf_add(&sb, line->buf + len, line->len - len); decode_header(mi, &sb); handle_content_transfer_encoding(mi, &sb); ret = 1; goto check_header_out; } if (cmp_header(line, "Message-Id")) { len = strlen("Message-Id: "); strbuf_add(&sb, line->buf + len, line->len - len); decode_header(mi, &sb); if (mi->add_message_id) mi->message_id = strbuf_detach(&sb, NULL); ret = 1; goto check_header_out; } check_header_out: strbuf_release(&sb); return ret; }
bool message_search_more_get_decoded(struct message_search_context *ctx, struct message_block *raw_block, struct message_block *decoded_block_r) { struct message_header_line *hdr = raw_block->hdr; struct message_block decoded_block; memset(decoded_block_r, 0, sizeof(*decoded_block_r)); decoded_block_r->part = raw_block->part; if (raw_block->part != ctx->prev_part) { /* part changes. we must change this before looking at content type */ message_search_reset(ctx); ctx->prev_part = raw_block->part; if (hdr == NULL) { /* we're returning to a multipart message. */ ctx->content_type_text = FALSE; } } if (hdr != NULL) { handle_header(ctx, hdr); if ((ctx->flags & MESSAGE_SEARCH_FLAG_SKIP_HEADERS) != 0) { /* we want to search only message bodies, but but decoder needs some headers so that it can decode the body properly. */ if (hdr->name_len != 12 && hdr->name_len != 25) return FALSE; if (strcasecmp(hdr->name, "Content-Type") != 0 && strcasecmp(hdr->name, "Content-Transfer-Encoding") != 0) return FALSE; } } else { /* body */ if (!ctx->content_type_text) return FALSE; } if (!message_decoder_decode_next_block(ctx->decoder, raw_block, &decoded_block)) return FALSE; if (decoded_block.hdr != NULL && (ctx->flags & MESSAGE_SEARCH_FLAG_SKIP_HEADERS) != 0) { /* Content-* header */ return FALSE; } *decoded_block_r = decoded_block; return message_search_more_decoded2(ctx, &decoded_block); }
static void cb_read(struct bufferevent *bev, void *arg) { struct request_ctx *req = arg; char *line; size_t n; if (req->read_state == READ_NONE) { req->read_state = READ_STATUS; } while (req->read_state == READ_STATUS || req->read_state == READ_HEADERS) { line = read_line(bev, &n); if (line == NULL) { return; } if (req->read_state == READ_STATUS) { verbose(VERBOSE, "%s(): status line: '%s'\n", __func__, line); parse_status(req, line, n); set_read_state(req, READ_HEADERS); } else { while (line && req->read_state == READ_HEADERS) { if (*line == '\0') { set_read_state(req, READ_BODY); } else { char *key, *val; verbose(VERBOSE, "%s(): header line '%s'\n", __func__, line); header_keyval(&key, &val, line); handle_header(req, key, val); free(line); line = read_line(bev, &n); } } } free(line); } while (req->read_state == READ_BODY && evbuffer_get_length(bufferevent_get_input(bev)) > 0) { drain_body(req, bev); } if (req->read_state == READ_DONE) { request_done(req, bev); } }
static int check_inbody_header(struct mailinfo *mi, const struct strbuf *line) { if (mi->inbody_header_accum.len && (line->buf[0] == ' ' || line->buf[0] == '\t')) { if (mi->use_scissors && is_scissors_line(line->buf)) { /* * This is a scissors line; do not consider this line * as a header continuation line. */ flush_inbody_header_accum(mi); return 0; } strbuf_strip_suffix(&mi->inbody_header_accum, "\n"); strbuf_addbuf(&mi->inbody_header_accum, line); return 1; } flush_inbody_header_accum(mi); if (starts_with(line->buf, ">From") && isspace(line->buf[5])) return is_format_patch_separator(line->buf + 1, line->len - 1); if (starts_with(line->buf, "[PATCH]") && isspace(line->buf[7])) { int i; for (i = 0; header[i]; i++) if (!strcmp("Subject", header[i])) { handle_header(&mi->s_hdr_data[i], line); return 1; } return 0; } if (is_inbody_header(mi, line)) { strbuf_addbuf(&mi->inbody_header_accum, line); return 1; } return 0; }
bool ContextBase::parseCmdArgs(int argc, char **argv, int skipFirstArgs) { for (_i=_skipFirstArgs; _i < argc; _i++) { if (isUsed(_i - _skipFirstArgs)) { continue; } if (strcmp(_argv[_i], "-i") == 0) { if (!handle_i()) return false; } else if (strcmp(_argv[_i], "-g") == 0) { if (!handle_g()) return false; } else if ((strcmp(_argv[_i], "-h") == 0) || (strcmp(_argv[_i], "--help") == 0)) { if (!handle_h()) return false; } else if (strcmp(_argv[_i], "-split") == 0) { if (!handle_split()) return false; } else if (strcmp(_argv[_i], "-bed") == 0) { if (!handle_bed()) return false; } else if (strcmp(_argv[_i], "-ubam") == 0) { if (!handle_ubam()) return false; } else if (strcmp(_argv[_i], "-fbam") == 0) { if (!handle_fbam()) return false; } else if(strcmp(_argv[_i], "-sorted") == 0) { if (!handle_sorted()) return false; } else if (strcmp(_argv[_i], "-nobuf") == 0) { if (!handle_nobuf()) return false; } else if (strcmp(_argv[_i], "-iobuf") == 0) { if (!handle_iobuf()) return false; } else if (strcmp(_argv[_i], "-prec") == 0) { if (!handle_prec()) return false; } else if (strcmp(_argv[_i], "-header") == 0) { if (!handle_header()) return false; } else if (strcmp(_argv[_i], "-n") == 0) { if (!handle_n()) return false; } else if (strcmp(_argv[_i], "-seed") == 0) { if (!handle_seed()) return false; } else if ((strcmp(_argv[_i], "-o") == 0) || (strcmp(_argv[_i], "-ops") == 0)) { if (!handle_o()) return false; } else if ((strcmp(_argv[_i], "-c") == 0) || (strcmp(_argv[_i], "-opCols") == 0)) { if (!handle_c()) return false; } else if (strcmp(_argv[_i], "-null") == 0) { if (!handle_null()) return false; } else if (strcmp(_argv[_i], "-delim") == 0) { if (!handle_delim()) return false; } else if (strcmp(_argv[_i], "-sortout") == 0) { if (!handle_sortout()) return false; } else if (strcmp(_argv[_i], "-nonamecheck") == 0) { if (!handle_nonamecheck()) return false; } } return true; }
static int httprecv(int sfd, struct string* buf, int* httpres) { char* b; ssize_t bl, bptr = 0; size_t i; struct string oneline; int retval = HTTP_EOK; int BUFSIZE = 8192; int isfirsthdr = 1; if ((b = malloc(BUFSIZE)) == NULL) return HTTP_ENOMEM; string_init(&oneline, 512, 512); while(1) { bl = recv(sfd, b + bptr, BUFSIZE - bptr, 0); if (bl == 0) { if (bptr == 0) { break; } } else if (bl < 0) { retval = HTTP_ENETERR; goto bail_out; } bptr += bl; if ((*b == '\n') || (*b == '\r')) goto finished; for (i = 0; i < bptr; i++) { if (b[i] == '\n') { string_clear(&oneline); if (!string_concatb(&oneline, b, i)) { retval=HTTP_ENOMEM; goto bail_out; } if (oneline.s[oneline.length - 1] == '\r') --oneline.length; if (++i >= bptr) { retval=HTTP_ESRVERR; goto bail_out; } if (bptr != i) { memmove(b, b + i, bptr - i); } bptr -= i; if (isfirsthdr) { if (handle_header(&oneline, httpres)) { retval=HTTP_ESRVERR; goto bail_out; } isfirsthdr = 0; } break; } } } finished: if (isfirsthdr) { retval=HTTP_ESRVERR; goto bail_out; } bl = 0; if (bptr > 2) { if ((b[0] == '\r') || (b[0] == '\n')) { ++bl; if ((b[1] == '\r') || (b[1] == '\n')) { ++bl; } } } if (!string_concatb(buf, b + bl, bptr - bl)) { retval=HTTP_ENOMEM; goto bail_out; } while(1) { bl = recv(sfd, b, BUFSIZE, 0); if (bl == 0) break; if (bl < 0) { retval=HTTP_ENETERR; goto bail_out; } if (!string_concatb(buf, b, bl)) { retval=HTTP_ENOMEM; goto bail_out; } } if (retval == HTTP_EOK) if (*httpres != 200) retval = HTTP_ESRVERR; bail_out: close(sfd); string_free(&oneline); free(b); return retval; }
int main( int argc, char const *argv[]) { int serverSock ; int val = 1; struct sockaddr_in serverAddr ; struct sockaddr_in clientAddr ; socklen_t clientAddrLen = sizeof (clientAddr); char buffer [512]; char * uuid = random_uuid(); fprintf(stdout , "%s, %s\n" ,"Start", uuid); fprintf(stdout , "%s, %s\n" ,"Another", random_uuid()); serverSock = socket(AF_INET, SOCK_STREAM , 0 ); if (serverSock == -1 ) { fprintf (stderr, "%s\n" ,"open socket fail"); return - 1; } // Make sure the port can be immediately re-used if (setsockopt(serverSock, SOL_SOCKET , SO_REUSEADDR, &val , sizeof(val )) < 0 ) { fprintf (stderr, "%s\n" ,"can't reuse socket"); close (serverSock); return - 1; } memset(&serverAddr , 0 , sizeof (serverAddr)); serverAddr.sin_family = AF_INET; serverAddr.sin_addr .s_addr = htonl(INADDR_ANY ); serverAddr.sin_port = htons(12345); if (bind( serverSock, (struct sockaddr *) & serverAddr, sizeof(serverAddr )) != 0 ) { fprintf (stderr, "%s\n" ,"can't bind socket"); close (serverSock); return - 1; } // Listen to socket fprintf(stdout , "%s\n" ,"start listen port 12345"); if (listen( serverSock, 1024) < 0 ) { return - 1; } while(1){ //accept will wait for message coming fprintf (stdout, "%s\n" ,"wait accept"); int clientSock = accept(serverSock , ( struct sockaddr *) &clientAddr, &clientAddrLen ); int total_byte_cnt = 0; for (;;) { int byteCount = read(clientSock , buffer + total_byte_cnt, sizeof(buffer ) - total_byte_cnt - 1); if ( byteCount >= 0 ) { total_byte_cnt += byteCount; buffer [total_byte_cnt] = 0; /* fprintf (stdout, "[1] %d bytes read\n" , byteCount); fprintf (stdout, "%s\n" , "========================" ); fprintf (stdout, "%s\n" , buffer); fprintf (stdout, "%s\n" , "========================" ); */ // char *pch = strstr(buffer,"GET / HTTP/1.1"); char *Upgrade = extract_substring(buffer ,"Upgrade: ","\n"); char *Connection = extract_substring(buffer ,"Connection: ","\n"); //Sec-WebSocket-Protocol should be "chat" char *Sec_WebSocket_Protocol = extract_substring(buffer ,"Sec-WebSocket-Protocol: ","\n"); if (pch == NULL) printf("pch is null"); //printf("[%s]\n", Upgrade); //printf("[%s]\n", Connection); if ((pch == buffer) && ((strcmp(Upgrade,"websocket") == 0) || (strcmp(Upgrade,"Websocket") == 0)) && ((strcmp(Connection,"Upgrade") == 0) || (strcmp(Connection,"keep-alive, Upgrade") == 0)) ){ handle_header(clientSock, buffer); handle_message_loop(clientSock); } // break; } else { //fprintf (stdout, "%s\n" ,"[3] oh no"); break; } } } fprintf(stdout , "%s\n" ,"Finish"); return 0; }
int main(int argc, char **argv) { state.line_num = -1; int c; while ((c = getopt(argc, argv, "o:")) != -1) { switch (c) { case 'o': if (output_path != NULL) { error(ERROR_GENERAL, "An output path was already selected."); } output_path = optarg; trace(TRACE_GENERAL, "Loading an output path of %s", output_path); break; } } if (output_path == NULL) { error(ERROR_GENERAL, "An output path must be provided for the generated bindings"); } state.line_num = 0; while (start_line()) { // identify line type if (state.token != NULL) { // we have input if (strcmp(state.token, keyword_comment) == 0) { while (next_token()) {} // nothing to do here, jump to the next line } else if (strcmp(state.token, keyword_include) == 0) { handle_header(); } else if (strcmp (state.token, keyword_userdata) == 0){ handle_userdata(); } else if (strcmp (state.token, keyword_singleton) == 0){ handle_singleton(); } else { error(ERROR_UNKNOWN_KEYWORD, "Expected a keyword, got: %s", state.token); } if (next_token()) { error(ERROR_UNKNOWN_KEYWORD, "Extra token provided: %s", state.token); } } } state.line_num = -1; char *file_name = (char *)allocate(strlen(output_path) + 5); sprintf(file_name, "%s.cpp", output_path); source = fopen(file_name, "w"); if (source == NULL) { error(ERROR_GENERAL, "Unable to open the output source file: %s", file_name); } fprintf(source, "// auto generated bindings, don't manually edit\n"); trace(TRACE_GENERAL, "Sanity checking parsed input"); sanity_check_userdata(); fprintf(source, "#include \"lua_generated_bindings.h\"\n"); trace(TRACE_GENERAL, "Starting emission"); emit_headers(source); fprintf(source, "\n\n"); emit_userdata_allocators(); emit_userdata_checkers(); emit_userdata_fields(); emit_userdata_methods(); emit_userdata_metatables(); emit_singleton_methods(); emit_singleton_metatables(); emit_loaders(); emit_sandbox(); fclose(source); source = NULL; sprintf(file_name, "%s.h", output_path); header = fopen(file_name, "w"); if (header == NULL) { error(ERROR_GENERAL, "Unable to open the output header file: %s", file_name); } free(file_name); fprintf(header, "#pragma once\n"); fprintf(header, "// auto generated bindings, don't manually edit\n"); emit_headers(header); fprintf(header, "#include \"lua/src/lua.hpp\"\n"); fprintf(header, "#include <new>\n\n"); emit_userdata_declarations(); fprintf(header, "void load_generated_bindings(lua_State *L);\n"); fprintf(header, "void load_generated_sandbox(lua_State *L);\n"); fclose(header); header = NULL; return 0; }