static int do_func(char **fields, int nfields, char **err) { char *name; char *rtype; char *atype; char *hdrs; char *envs; struct sym_test *st; if (nfields != 5) { myasprintf(err, "number of fields (%d) != 5", nfields); return (-1); } name = fields[0]; rtype = fields[1]; atype = fields[2]; hdrs = fields[3]; envs = fields[4]; st = myzalloc(sizeof (*st)); st->st_type = SYM_FUNC; st->st_name = mystrdup(name); st->st_rtype = mystrdup(rtype); if ((add_envs(st, envs, err) < 0) || (add_headers(st, hdrs, err) < 0) || (add_arg_types(st, atype, err) < 0)) { return (-1); } append_sym_test(st); return (0); }
static int do_value(char **fields, int nfields, char **err) { char *name; char *type; char *hdrs; char *envs; struct sym_test *st; if (nfields != 4) { myasprintf(err, "number of fields (%d) != 4", nfields); return (-1); } name = fields[0]; type = fields[1]; hdrs = fields[2]; envs = fields[3]; st = myzalloc(sizeof (*st)); st->st_type = SYM_VALUE; st->st_name = mystrdup(name); st->st_rtype = mystrdup(type); if ((add_envs(st, envs, err) < 0) || (add_headers(st, hdrs, err) < 0)) { return (-1); } append_sym_test(st); return (0); }
static int do_type(char **fields, int nfields, char **err) { char *decl; char *hdrs; char *envs; struct sym_test *st; if (nfields != 3) { myasprintf(err, "number of fields (%d) != 3", nfields); return (-1); } decl = fields[0]; hdrs = fields[1]; envs = fields[2]; st = myzalloc(sizeof (*st)); st->st_type = SYM_TYPE; st->st_name = mystrdup(decl); st->st_rtype = mystrdup(decl); if ((add_envs(st, envs, err) < 0) || (add_headers(st, hdrs, err) < 0)) { return (-1); } append_sym_test(st); return (0); }
int message_complete_cb(http_parser* parser) { struct request_info *r = parser->data; struct stat file_stat; int exists; char datestring[256]; struct tm *tm; if (strstr(r->file, "..") != NULL) { send_error(r, 404); return 0; } exists = stat(r->file, &file_stat); if (exists != 0){ switch (errno){ case EACCES: send_error(r, 403); break; default: send_error(r, 404); break; } return 0; } else { /* The file exists */ /* Test if it's a folder */ if (S_ISDIR(file_stat.st_mode) > 0) { send_error(r, 404); return 0; } tm = localtime(&file_stat.st_mtime); /* Get localized date string. */ strftime(datestring, sizeof(datestring), DATE_FORMAT, tm); evbuffer_add_printf(r->buf, "HTTP/1.0 200 OK\r\n"); evbuffer_add_printf(r->buf, "Content-Length: %9jd\r\n", (intmax_t)file_stat.st_size); evbuffer_add_printf(r->buf, "Last-Modified: %s\r\n", datestring); add_headers(r->buf); } if (parser->method == HTTP_HEAD) { event_add(&r->wr_ev, NULL); } else { // Open the file for parsing to body if (read_from_file(r) == -1) { evbuffer_drain(r->buf, EVBUFFER_LENGTH(r->buf)); send_error(r, 500); return 0; } // Send reponse with body now :) event_add(&r->wr_ev, NULL); } printf("complete\n"); return 0; }
SRef<Sip_Request*> Sip_Request::create_sip_message_invite( const std::string &call_id, const Sip_Uri &to_uri, const Sip_Uri &from_uri, const Sip_Uri &contact, int seq_no, SRef<Sip_Stack*> stack ) { SRef<Sip_Request*> req = new Sip_Request("INVITE", to_uri); req->add_default_headers( from_uri, to_uri, seq_no, call_id ); add_headers(req, contact, stack); return req; }
void _sal_op_add_custom_headers(SalOp *op, belle_sip_message_t *msg){ if (op->base.sent_custom_headers){ belle_sip_message_t *ch=(belle_sip_message_t*)op->base.sent_custom_headers; belle_sip_list_t *l=belle_sip_message_get_all_headers(ch); belle_sip_list_t *elem; for(elem=l;elem!=NULL;elem=elem->next){ add_headers(op,(belle_sip_header_t*)elem->data,msg); } belle_sip_list_free(l); } }
void send_error(int32 fd, int32 status, const int8 *title, const int8 *extra_header, const int8 *text) { int32 len = 0; int32 str_len = 0; int8 buf[BUFFSIZ]; len += add_headers(buf, status, title, extra_header, "text/html"); str_len = len; len += snprintf_s_isis(&buf[len], (BUFFSIZ - str_len), "<HTML>" "<HEAD><TITLE>%d %s</TITLE></HEAD>\n" "<BODY BGCOLOR=\"#cc9999\">" "<H4>%d %s</H4>\n", status, (char *)title, status, (char *)title); str_len = len; len += snprintf_s_s(&buf[len], (BUFFSIZ - str_len), "%s\n", (char *)text); str_len = len; len += snprintf(&buf[len], (BUFFSIZ - str_len), "</BODY></HTML>\n"); http_write(fd, buf, len); }
static int do_define(char **fields, int nfields, char **err) { char *name, *value, *hdrs, *envs; struct sym_test *st; if (nfields != 4) { myasprintf(err, "number of fields (%d) != 4", nfields); return (-1); } name = fields[0]; value = fields[1]; hdrs = fields[2]; envs = fields[3]; st = myzalloc(sizeof (*st)); st->st_type = SYM_DEFINE; st->st_name = mystrdup(name); /* * A value to compare against is optional. trim will leave it as a null * pointer if there's nothing there. */ test_trim(&value); if (*value != '\0') st->st_defval = mystrdup(value); if ((add_envs(st, envs, err) < 0) || (add_headers(st, hdrs, err) < 0)) { return (-1); } append_sym_test(st); return (0); }
/* @name starts from "/", such as "/index.html" */ void send_file(int32 sockfd, const int8 *name) { int32 fd; int32 r, len; int64 filesize; struct stat st; int8 path[1024], contentlen[128], buf[BUFFSIZ]; snprintf_s_ss(path, sizeof(path), "%s%s", HTTPD_WORK_DIR, (char *)name); if (stat(path, &st) < 0 || (fd = open(path, O_RDONLY)) < 0) { send_error(sockfd, 404, "Not Found", NULL, "File not Found."); close(sockfd); HTTPD_ERR("Not found file %s\n", path); return; } filesize = st.st_size; snprintf_s_ll(contentlen, sizeof(contentlen), "Content-Length: %lld", filesize); len = add_headers(buf, 200, "OK", contentlen, file_mime_type(name)); http_write(sockfd, buf, len); while (filesize > 0) { r = read(fd, buf, sizeof(buf)); if (r < 0 && (errno == EINTR || errno == EAGAIN)) continue; if (r <= 0 || http_write(sockfd, buf, r) != r) break; filesize -= r; } close(fd); }
int hpquads(startree_t* starkd, codefile_t* codes, quadfile_t* quads, int Nside, double scale_min_arcmin, double scale_max_arcmin, int dimquads, int passes, int Nreuses, int Nloosen, int id, anbool scanoccupied, void* sort_data, int (*sort_func)(const void*, const void*), int sort_size, char** args, int argc) { hpquads_t myhpquads; hpquads_t* me = &myhpquads; int i; int pass; anbool circle = TRUE; double radius2; il* hptotry; int Nhptotry = 0; int nquads; double hprad; double quadscale; int skhp, sknside; qfits_header* qhdr; qfits_header* chdr; int N; int dimcodes; int quadsize; int NHP; memset(me, 0, sizeof(hpquads_t)); if (Nside > HP_MAX_INT_NSIDE) { ERROR("Error: maximum healpix Nside = %i", HP_MAX_INT_NSIDE); return -1; } if (Nreuses > 255) { ERROR("Error, reuse (-r) must be less than 256"); return -1; } me->Nside = Nside; me->dimquads = dimquads; NHP = 12 * Nside * Nside; dimcodes = dimquad2dimcode(dimquads); quadsize = sizeof(unsigned int) * dimquads; logmsg("Nside=%i. Nside^2=%i. Number of healpixes=%i. Healpix side length ~ %g arcmin.\n", me->Nside, me->Nside*me->Nside, NHP, healpix_side_length_arcmin(me->Nside)); me->sort_data = sort_data; me->sort_func = sort_func; me->sort_size = sort_size; tic(); me->starkd = starkd; N = startree_N(me->starkd); logmsg("Star tree contains %i objects.\n", N); // get the "HEALPIX" header from the skdt... skhp = qfits_header_getint(startree_header(me->starkd), "HEALPIX", -1); if (skhp == -1) { if (!qfits_header_getboolean(startree_header(me->starkd), "ALLSKY", FALSE)) { logmsg("Warning: skdt does not contain \"HEALPIX\" header. Code and quad files will not contain this header either.\n"); } } // likewise "HPNSIDE" sknside = qfits_header_getint(startree_header(me->starkd), "HPNSIDE", 1); if (sknside && Nside % sknside) { logerr("Error: Nside (-n) must be a multiple of the star kdtree healpixelisation: %i\n", sknside); return -1; } if (!scanoccupied && (N*(skhp == -1 ? 1 : sknside*sknside*12) < NHP)) { logmsg("\n\n"); logmsg("NOTE, your star kdtree is sparse (has only a fraction of the stars expected)\n"); logmsg(" so you probably will get much faster results by setting the \"-E\" command-line\n"); logmsg(" flag.\n"); logmsg("\n\n"); } quads->dimquads = me->dimquads; codes->dimcodes = dimcodes; quads->healpix = skhp; codes->healpix = skhp; quads->hpnside = sknside; codes->hpnside = sknside; if (id) { quads->indexid = id; codes->indexid = id; } qhdr = quadfile_get_header(quads); chdr = codefile_get_header(codes); add_headers(qhdr, args, argc, startree_header(me->starkd), circle, passes); add_headers(chdr, args, argc, startree_header(me->starkd), circle, passes); if (quadfile_write_header(quads)) { ERROR("Couldn't write headers to quad file"); return -1; } if (codefile_write_header(codes)) { ERROR("Couldn't write headers to code file"); return -1; } quads->numstars = codes->numstars = N; me->quad_dist2_upper = arcmin2distsq(scale_max_arcmin); me->quad_dist2_lower = arcmin2distsq(scale_min_arcmin); codes->index_scale_upper = quads->index_scale_upper = distsq2rad(me->quad_dist2_upper); codes->index_scale_lower = quads->index_scale_lower = distsq2rad(me->quad_dist2_lower); me->nuses = calloc(N, sizeof(unsigned char)); // hprad = sqrt(2) * (healpix side length / 2.) hprad = arcmin2dist(healpix_side_length_arcmin(Nside)) * M_SQRT1_2; quadscale = 0.5 * sqrt(me->quad_dist2_upper); // 1.01 for a bit of safety. we'll look at a few extra stars. radius2 = square(1.01 * (hprad + quadscale)); me->radius2 = radius2; logmsg("Healpix radius %g arcsec, quad scale %g arcsec, total %g arcsec\n", distsq2arcsec(hprad*hprad), distsq2arcsec(quadscale*quadscale), distsq2arcsec(radius2)); hptotry = il_new(1024); if (scanoccupied) { logmsg("Scanning %i input stars...\n", N); for (i=0; i<N; i++) { double xyz[3]; int j; if (startree_get(me->starkd, i, xyz)) { ERROR("Failed to get star %i", i); return -1; } j = xyzarrtohealpix(xyz, Nside); il_insert_unique_ascending(hptotry, j); if (log_get_level() > LOG_VERB) { double ra,dec; if (startree_get_radec(me->starkd, i, &ra, &dec)) { ERROR("Failed to get RA,Dec for star %i\n", i); return -1; } logdebug("star %i: RA,Dec %g,%g; xyz %g,%g,%g; hp %i\n", i, ra, dec, xyz[0], xyz[1], xyz[2], j); } } logmsg("Will check %zu healpixes.\n", il_size(hptotry)); if (log_get_level() > LOG_VERB) { logdebug("Checking healpixes: [ "); for (i=0; i<il_size(hptotry); i++) logdebug("%i ", il_get(hptotry, i)); logdebug("]\n"); } } else { if (skhp == -1) { // Try all healpixes. il_free(hptotry); hptotry = NULL; Nhptotry = NHP; } else { // The star kdtree may itself be healpixed int starhp, starx, stary; // In that case, the healpixes we are interested in form a rectangle // within a big healpix. These are the coords (in [0, Nside)) of // that rectangle. int x0, x1, y0, y1; int x, y; healpix_decompose_xy(skhp, &starhp, &starx, &stary, sknside); x0 = starx * (Nside / sknside); x1 = (starx+1) * (Nside / sknside); y0 = stary * (Nside / sknside); y1 = (stary+1) * (Nside / sknside); for (y=y0; y<y1; y++) { for (x=x0; x<x1; x++) { int j = healpix_compose_xy(starhp, x, y, Nside); il_append(hptotry, j); } } assert(il_size(hptotry) == (Nside/sknside) * (Nside/sknside)); } } if (hptotry) Nhptotry = il_size(hptotry); me->quadlist = bl_new(65536, quadsize); if (Nloosen) me->retryhps = il_new(1024); for (pass=0; pass<passes; pass++) { char key[64]; int nthispass; logmsg("Pass %i of %i.\n", pass+1, passes); logmsg("Trying %i healpixes.\n", Nhptotry); nthispass = build_quads(me, Nhptotry, hptotry, Nreuses); logmsg("Made %i quads (out of %i healpixes) this pass.\n", nthispass, Nhptotry); logmsg("Made %i quads so far.\n", (me->bigquadlist ? bt_size(me->bigquadlist) : 0) + (int)bl_size(me->quadlist)); sprintf(key, "PASS%i", pass+1); fits_header_mod_int(chdr, key, nthispass, "quads created in this pass"); fits_header_mod_int(qhdr, key, nthispass, "quads created in this pass"); logmsg("Merging quads...\n"); if (!me->bigquadlist) me->bigquadlist = bt_new(quadsize, 256); for (i=0; i<bl_size(me->quadlist); i++) { void* q = bl_access(me->quadlist, i); bt_insert2(me->bigquadlist, q, FALSE, compare_quads, &me->dimquads); } bl_remove_all(me->quadlist); } il_free(hptotry); hptotry = NULL; if (Nloosen) { int R; for (R=Nreuses+1; R<=Nloosen; R++) { il* trylist; int nthispass; logmsg("Loosening reuse maximum to %i...\n", R); logmsg("Trying %zu healpixes.\n", il_size(me->retryhps)); if (!il_size(me->retryhps)) break; trylist = me->retryhps; me->retryhps = il_new(1024); nthispass = build_quads(me, il_size(trylist), trylist, R); logmsg("Made %i quads (out of %zu healpixes) this pass.\n", nthispass, il_size(trylist)); il_free(trylist); for (i=0; i<bl_size(me->quadlist); i++) { void* q = bl_access(me->quadlist, i); bt_insert2(me->bigquadlist, q, FALSE, compare_quads, &me->dimquads); } bl_remove_all(me->quadlist); } } if (me->retryhps) il_free(me->retryhps); kdtree_free_query(me->res); me->res = NULL; me->inds = NULL; me->stars = NULL; free(me->nuses); me->nuses = NULL; logmsg("Writing quads...\n"); // add the quads from the big-quadlist nquads = bt_size(me->bigquadlist); for (i=0; i<nquads; i++) { unsigned int* q = bt_access(me->bigquadlist, i); quad_write(codes, quads, q, me->starkd, me->dimquads, dimcodes); } // add the quads that were made during the final round. for (i=0; i<bl_size(me->quadlist); i++) { unsigned int* q = bl_access(me->quadlist, i); quad_write(codes, quads, q, me->starkd, me->dimquads, dimcodes); } // fix output file headers. if (quadfile_fix_header(quads)) { ERROR("Failed to fix quadfile headers"); return -1; } if (codefile_fix_header(codes)) { ERROR("Failed to fix codefile headers"); return -1; } bl_free(me->quadlist); bt_free(me->bigquadlist); toc(); logmsg("Done.\n"); return 0; }
void http_seed_connection::write_request(peer_request const& r) { INVARIANT_CHECK; boost::shared_ptr<torrent> t = associated_torrent().lock(); TORRENT_ASSERT(t); TORRENT_ASSERT(t->valid_metadata()); // http_seeds don't support requesting more than one piece // at a time TORRENT_ASSERT(r.length <= t->torrent_file().piece_size(r.piece)); std::string request; request.reserve(400); int size = r.length; const int block_size = t->block_size(); const int piece_size = t->torrent_file().piece_length(); peer_request pr; while (size > 0) { int request_offset = r.start + r.length - size; pr.start = request_offset % piece_size; pr.length = (std::min)(block_size, size); pr.piece = r.piece + request_offset / piece_size; m_requests.push_back(pr); size -= pr.length; } int proxy_type = m_settings.get_int(settings_pack::proxy_type); bool using_proxy = (proxy_type == settings_pack::http || proxy_type == settings_pack::http_pw) && !m_ssl; request += "GET "; request += using_proxy ? m_url : m_path; request += "?info_hash="; request += escape_string(reinterpret_cast<char const*>(&t->torrent_file().info_hash()[0]), 20); request += "&piece="; request += to_string(r.piece).elems; // if we're requesting less than an entire piece we need to // add ranges if (r.start > 0 || r.length != t->torrent_file().piece_size(r.piece)) { request += "&ranges="; request += to_string(r.start).elems; request += "-"; // ranges are inclusive, just like HTTP request += to_string(r.start + r.length - 1).elems; } request += " HTTP/1.1\r\n"; add_headers(request, m_settings, using_proxy); request += "\r\n\r\n"; m_first_request = false; #ifndef TORRENT_DISABLE_LOGGING peer_log(peer_log_alert::outgoing_message, "REQUEST", "%s", request.c_str()); #endif send_buffer(request.c_str(), request.size(), message_type_request); }
/*--------------------------------------------------------------*/ int main( int main_argc, char **main_argv) { clock_t startClock = clock(); /*--------------------------------------------------------------*/ /* Non-function definitions. */ /*--------------------------------------------------------------*/ struct command_line_object *command_line; struct tec_object *tec; struct world_object *world; struct world_output_file_object *output; struct world_output_file_object *growth_output; char *prefix; /*--------------------------------------------------------------*/ /* Local Function declarations */ /*--------------------------------------------------------------*/ struct command_line_object *construct_command_line( int, char **); struct world_object *construct_world( struct command_line_object *); struct world_output_file_object *construct_output_files( char *, struct command_line_object *); struct tec_object *construct_tec( struct command_line_object *, struct world_object * ); void execute_tec( struct tec_object *, struct command_line_object *, struct world_output_file_object *, struct world_output_file_object *, struct world_object *); void destroy_tec( struct tec_object * ); void destroy_output_files( struct command_line_object *, struct world_output_file_object *); void destroy_world( struct command_line_object *, struct world_object *); void destroy_command_line( struct command_line_object * ); void add_headers( struct world_output_file_object *, struct command_line_object * ); void add_growth_headers( struct world_output_file_object *, struct command_line_object * ); srand((unsigned)(time(0))); /*--------------------------------------------------------------*/ /* Command line parsing. */ /*--------------------------------------------------------------*/ command_line = construct_command_line(main_argc, main_argv); /*--------------------------------------------------------------*/ /* Check if print version flag was set. If so, just print out */ /* the version and return. */ /*--------------------------------------------------------------*/ if (command_line[0].version_flag > 0 ) { printf("RHESSys Version: %s\n", RHESSYS_VERSION); return(EXIT_SUCCESS); } if (command_line[0].verbose_flag > 0 ) fprintf(stderr,"FINISHED CON COMMAND LINE ***\n"); /*--------------------------------------------------------------*/ /* Construct the world object. */ /*--------------------------------------------------------------*/ world = construct_world( command_line ); if (command_line[0].verbose_flag > 0 ) fprintf(stderr,"FINISHED CON WORLD ***\n"); /*--------------------------------------------------------------*/ /* Construct the output file objects. */ /*--------------------------------------------------------------*/ /*--------------------------------------------------------------*/ /* Make up the prefix for the output files. */ /*--------------------------------------------------------------*/ prefix = (char *)calloc(256, sizeof(char)); if ( command_line[0].output_prefix != NULL ){ strcpy(prefix,command_line[0].output_prefix); } else{ strcpy(prefix,PRE); } output = construct_output_files( prefix, command_line ); if (command_line[0].grow_flag > 0) { strcat(prefix,"_grow"); growth_output = construct_output_files(prefix, command_line ); } else growth_output = NULL; add_headers(output, command_line); if (command_line[0].grow_flag > 0) add_growth_headers(growth_output, command_line); if(command_line[0].verbose_flag > 0 ) fprintf(stderr,"FINISHED CON OUTPUT\n"); /*--------------------------------------------------------------*/ /* Create the tec object (temporal event control) */ /* */ /* This object specifies temporal events such as output. */ /*--------------------------------------------------------------*/ tec = construct_tec( command_line, world); if (command_line[0].verbose_flag > 0 ) fprintf(stderr,"FINISHED CON TEC\n"); /*--------------------------------------------------------------*/ /* AN EVENT LOOP WOULD GO HERE. */ /*--------------------------------------------------------------*/ fprintf(stderr,"Beginning Simulation\n"); execute_tec( tec, command_line, output, growth_output, world ); if (command_line[0].verbose_flag > 0 ) fprintf(stderr,"FINISHED EXE TEC\n"); /*--------------------------------------------------------------*/ /* Destroy the tec object. */ /*--------------------------------------------------------------*/ destroy_tec( tec ); if (command_line[0].verbose_flag > 0 ) fprintf(stderr,"FINISHED DES TEC\n"); /*--------------------------------------------------------------*/ /* Destroy output file objects (close them) */ /*--------------------------------------------------------------*/ destroy_output_files( command_line, output ); if (command_line[0].grow_flag > 0) destroy_output_files( command_line, growth_output ); if (command_line[0].verbose_flag > 0 ) fprintf(stderr,"FINISHED DES OUTPUT FILES\n"); /*--------------------------------------------------------------*/ /* Destroy the world. */ /*--------------------------------------------------------------*/ destroy_world(command_line, world ); if (command_line[0].verbose_flag > 0 ) fprintf(stderr,"FINISHED DES WORLD\n"); /*--------------------------------------------------------------*/ /* Destroy the command_line_object */ /*--------------------------------------------------------------*/ destroy_command_line( command_line ); if (command_line[0].verbose_flag > 0 ) fprintf(stderr,"FINISHED DES COMMAND LINE\n"); /*--------------------------------------------------------------*/ /* The end. */ /*--------------------------------------------------------------*/ clock_t endClock =clock(); printf("\ntime cost = %ld seconds\n",(endClock - startClock)/CLOCKS_PER_SEC); return(EXIT_SUCCESS); } /*end main*/