// return at most n nodes, if theres nodes less than n, return size is also less than n. // find(hash) :: lock_service -> key -> [node] where hash(node0) <= hash(key) < hash(node1) bool cht::find(const std::string& key, std::vector<std::pair<std::string,int> >& out, size_t n){ out.clear(); std::vector<std::string> hlist; if(! get_hashlist_(key, hlist)){ throw JUBATUS_EXCEPTION(not_found(key)); } std::string hash = make_hash(key); std::string path = ACTOR_BASE_PATH + "/" + name_ + "/cht"; std::vector<std::string>::iterator node0 = std::lower_bound(hlist.begin(), hlist.end(), hash); size_t idx = int(node0 - hlist.begin()) % hlist.size(); std::string loc; for(size_t i=0; i<n; ++i){ std::string ip; int port; if(lock_service_->read(path + "/" + hlist[idx], loc)){ revert(loc, ip, port); out.push_back(make_pair(ip,port)); }else{ // TODO: output log throw JUBATUS_EXCEPTION(not_found(path)); } idx++; idx %= hlist.size(); } return !hlist.size(); }
int cmd_main(int argc, const char **argv) { char *method = getenv("REQUEST_METHOD"); char *dir; struct service_cmd *cmd = NULL; char *cmd_arg = NULL; int i; struct strbuf hdr = STRBUF_INIT; set_die_routine(die_webcgi); set_die_is_recursing_routine(die_webcgi_recursing); if (!method) die("No REQUEST_METHOD from server"); if (!strcmp(method, "HEAD")) method = "GET"; dir = getdir(); for (i = 0; i < ARRAY_SIZE(services); i++) { struct service_cmd *c = &services[i]; regex_t re; regmatch_t out[1]; if (regcomp(&re, c->pattern, REG_EXTENDED)) die("Bogus regex in service table: %s", c->pattern); if (!regexec(&re, dir, 1, out, 0)) { size_t n; if (strcmp(method, c->method)) return bad_request(&hdr, c); cmd = c; n = out[0].rm_eo - out[0].rm_so; cmd_arg = xmemdupz(dir + out[0].rm_so + 1, n - 1); dir[out[0].rm_so] = 0; break; } regfree(&re); } if (!cmd) not_found(&hdr, "Request not supported: '%s'", dir); setup_path(); if (!enter_repo(dir, 0)) not_found(&hdr, "Not a git repository: '%s'", dir); git_config(git_default_config, NULL); if (!getenv("GIT_HTTP_EXPORT_ALL") && access("git-daemon-export-ok", F_OK) ) not_found(&hdr, "Repository not exported: '%s'", dir); http_config(); max_request_buffer = git_env_ulong("GIT_HTTP_MAX_REQUEST_BUFFER", max_request_buffer); cmd->imp(&hdr, cmd_arg); return 0; }
//construct the real url in the header->url int make_real_url(http_header* pheader,char* path,int cli_fd) { int cgi; //char path[512]; char* query_string = NULL; serv_static(cli_fd,pheader->url); printf("runing this place ! \n"); // to find char'?' to decided if need CGI if( strcmp(pheader->method,"GET") ==0 ) { query_string = pheader->url; while( (*(query_string) != '?') && (*query_string != '\0')) query_string++; if(*query_string == '?') { //set the cgi flag,the program need cgi cgi = 1; //split the string *query_string = '\0'; //the back part url used to execute cgi query_string++; } } //sprintf(),end at the char '\0' sprintf(path,"htdocs%s",pheader->url); size_t len = strlen(path); if( path[len-1] == '/') strcat(path,"index.html"); //check if the file exits struct stat st_info; if(-1 == stat(path,&st_info) ) { //debug_info() not_found(cli_fd); } else{ //file is a dir,strcat() file with the dir //than path is the real url if((st_info.st_mode & S_IFMT) == S_IFDIR) strcat(path,"/index.html"); printf("the file path:%s \n",path); serv_static(cli_fd, path); //if the file is executed ....extensions //set the cgi =1 //than execute(cgi) } return 0; }
/* *************************************** * @描述:发送一个文件到客户端 * @输入:[in] client: 客户端文件描述符 * [in] filename:文件名 * @输出:无 * **************************************/ void serve_file(int client, const char *filename) { FILE *resource = NULL; int numchars = 1; char buf[1024]; /* 读取并丢弃header */ buf[0] = 'A'; buf[1] = '\0'; while((numchars > 0) && strcmp("\n", buf)) numchars = get_line(client, buf, sizeof(buf)); /* 打开server 文件 */ resource = fopen(filename, "r"); if(resource == NULL) not_found(client); else { /* 写HTTP header */ headers(client, filename); /* 复制文件 */ cat(client, resource); } fclose(resource); }
std::string const& SpeciesType::operator[](std::string const& name) const { string_map_type::const_iterator i(attrs_.find(name)); if (i == attrs_.end()) throw not_found((boost::format("key %s not found") % name).str()); return (*i).second; }
static void send_local_file(const char *the_type, const char *name) { const char *p = git_path("%s", name); size_t buf_alloc = 8192; char *buf = xmalloc(buf_alloc); int fd; struct stat sb; fd = open(p, O_RDONLY); if (fd < 0) not_found("Cannot open '%s': %s", p, strerror(errno)); if (fstat(fd, &sb) < 0) die_errno("Cannot stat '%s'", p); hdr_int(content_length, sb.st_size); hdr_str(content_type, the_type); hdr_date(last_modified, sb.st_mtime); end_headers(); for (;;) { ssize_t n = xread(fd, buf, buf_alloc); if (n < 0) die_errno("Cannot read '%s'", p); if (!n) break; write_or_die(1, buf, n); } close(fd); free(buf); }
/* Send html page to client*/ void send_page(int socket) { FILE * fp; /* Searchs for page in directory htdocs*/ sprintf(buf_tmp,"htdocs/%s",req_buf); #if DEBUG printf("send_page: searching for %s\n",buf_tmp); #endif /* Verifies if file exists*/ if((fp=fopen(buf_tmp,"rt"))==NULL) { /* Page not found, send error to client*/ printf("send_page: page %s not found, alerting client\n",buf_tmp); not_found(socket); } else { /* Page found, send to client*/ /* First send HTTP header back to client*/ send_header(socket); printf("send_page: sending page %s to client\n",buf_tmp); while(fgets(buf_tmp,SIZE_BUF,fp)) send(socket,buf_tmp,strlen(buf_tmp),0); /* Close file*/ fclose(fp); } return; }
void serve_file(int client, const char *filename) { FILE *resource = NULL; int numchars = 1; char buf[1024]; //确保 buf 里面有东西,能进入下面的 while 循环 buf[0] = 'A'; buf[1] = '\0'; //循环作用是读取并忽略掉这个 http 请求后面的所有内容 while ((numchars > 0) && strcmp("\n", buf)) /* read & discard headers */ numchars = get_line(client, buf, sizeof(buf)); //打开这个传进来的这个路径所指的文件 resource = fopen(filename, "r"); if (resource == NULL) not_found(client); else { //打开成功后,将这个文件的基本信息封装成 response 的头部(header)并返回 headers(client, filename); //接着把这个文件的内容读出来作为 response 的 body 发送到客户端 cat(client, resource); } fclose(resource); }
ErrorCode MeshTag::get_data(const SequenceManager*, Error* /* error */, const EntityHandle* entities, size_t num_entities, void* data) const { if (!all_root_set(get_name(), entities, num_entities)) return MB_TAG_NOT_FOUND; const void* ptr; int len; if (!mValue.empty()) { ptr = &mValue[0]; len = mValue.size(); } else if (get_default_value()) { ptr = get_default_value(); len = get_default_value_size(); } else { return not_found(get_name()); } SysUtil::setmem(data, ptr, len, num_entities); return MB_SUCCESS; }
ErrorCode MeshTag::get_data(const SequenceManager*, Error* /* error */, const EntityHandle* entities, size_t num_entities, const void** data_ptrs, int* data_lengths) const { const void* ptr; int len; if (!mValue.empty()) { ptr = &mValue[0]; len = mValue.size(); } else if (get_default_value()) { ptr = get_default_value(); len = get_default_value_size(); } else { return not_found(get_name()); } for (size_t i = 0; i < num_entities; ++i) { if (entities[i]) return not_root_set(get_name(), entities[i]); // Not root set data_ptrs[i] = ptr; if (data_lengths) data_lengths[i] = len; } return MB_SUCCESS; }
static void do_file_response ( struct HTTPRequest *req, /* HTTP request */ FILE *out, /* output fd */ char *docroot /* docroot path */ ) { struct FileInfo *info = NULL; dbg( "req=%p, out=%p, docroot=%p\n", req, out, docroot ); info = get_fileinfo( docroot, req->path ); if( 0 == info->ok ) { free_fileinfo( info ); not_found( req, out ); return ; } output_common_header_fields( req, out, "200 OK" ); fprintf( out, "Content-Length: %ld\r\n", info->size ); fprintf( out, "Content-Type: %s\r\n", guess_content_type( info ) ); fprintf( out, "\r\n" ); outputBodyFields( info, req, out ); fflush( out ); free_fileinfo( info ); }
ErrorCode DenseTag::get_data( const SequenceManager* seqman, Error* error, const EntityHandle* entities, size_t num_entities, const void** pointers, int* data_lengths ) const { ErrorCode result; const EntityHandle *const end = entities + num_entities; size_t junk; const unsigned char* ptr = NULL; // initialize to get rid of warning if (data_lengths) { const int len = get_size(); SysUtil::setmem( data_lengths, &len, sizeof(int), num_entities ); } for (const EntityHandle* i = entities; i != end; ++i, ++pointers) { result = get_array( seqman, error, *i, ptr, junk ); if (MB_SUCCESS != result) return result; if (ptr) *pointers = ptr; else if (get_default_value()) *pointers = get_default_value(); else return not_found( error, get_name(), *i ); } return MB_SUCCESS; }
ErrorCode DenseTag::get_data( const SequenceManager* seqman, Error* error, const Range& entities, void* values ) const { ErrorCode rval; size_t avail; const unsigned char* array = NULL; // initialize to get rid of warning unsigned char* data = reinterpret_cast<unsigned char*>(values); for (Range::const_pair_iterator p = entities.const_pair_begin(); p != entities.const_pair_end(); ++p) { EntityHandle start = p->first; while (start <= p->second) { rval = get_array( seqman, error, start, array, avail ); if (MB_SUCCESS != rval) return rval; const size_t count = std::min<size_t>(p->second - start + 1, avail); if (array) memcpy( data, array, get_size() * count ); else if (get_default_value()) SysUtil::setmem( data, get_default_value(), get_size(), count ); else return not_found( error, get_name(), start ); data += get_size() * count; start += count; } } return MB_SUCCESS; }
ErrorCode DenseTag::get_data( const SequenceManager* seqman, Error* error, const EntityHandle* entities, size_t num_entities, void* adata ) const { size_t junk; unsigned char* ptr = reinterpret_cast<unsigned char*>(adata); const EntityHandle *const end = entities + num_entities; for (const EntityHandle* i = entities; i != end; ++i, ptr += get_size()) { const unsigned char* data = 0; ErrorCode rval = get_array( seqman, error, *i, data, junk ); if (MB_SUCCESS != rval) return rval; if (data) memcpy( ptr, data, get_size() ); else if (get_default_value()) memcpy( ptr, get_default_value(), get_size() ); else return not_found( error, get_name(), *i ); } return MB_SUCCESS; }
//send the static file to client int serv_static(int cli_fd,char* filename) { const char* file_type; char* dot_pos = rindex(filename,'.'); file_type = get_filetype(dot_pos); //open the file,construct the head,send FILE* ind = fopen(filename,"r"); if(ind == NULL) { //debug_info printf("cannot open the file"); //404 not found not_found(cli_fd); return -1; } else { //construct the http response header make_header(cli_fd,filename); //than send the file char buff[1024]; memset(buff,0,sizeof(buff)); while(!feof(ind)) { fgets(buff,sizeof(buff),ind); send(cli_fd,buff,strlen(buff),0); } } return 0; }
TEST(Database, ctorExecCreateDropExist) { remove("test.db3"); { // Try to open an unexisting database EXPECT_THROW(SQLite::Database not_found("test.db3"), SQLite::Exception); // Create a new database SQLite::Database db("test.db3", SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); EXPECT_STREQ("test.db3", db.getFilename().c_str()); EXPECT_FALSE(db.tableExists("test")); EXPECT_FALSE(db.tableExists(std::string("test"))); EXPECT_EQ(0, db.getLastInsertRowid()); EXPECT_EQ(0, db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)")); EXPECT_TRUE(db.tableExists("test")); EXPECT_TRUE(db.tableExists(std::string("test"))); EXPECT_EQ(0, db.getLastInsertRowid()); EXPECT_EQ(0, db.exec("DROP TABLE IF EXISTS test")); EXPECT_FALSE(db.tableExists("test")); EXPECT_FALSE(db.tableExists(std::string("test"))); EXPECT_EQ(0, db.getLastInsertRowid()); } // Close DB test.db3 remove("test.db3"); }
/* 处理静态内容 */ void deal_static(int client_sock,char *path,int file_size){ //send_head(client_sock,path); //FILE *file=fopen(path,"r"); char *buf; char file[file_size]; //对buf进行内存分配 buf = (char *)malloc(file_size); //打开文件 int fd = open(path,O_RDONLY); if(fd==-1){ not_found(client_sock,path); }else{ //发送头部 send_head(client_sock,path); //获取内容 get_content(buf,fd,file_size); //给客户端发送文件内容 send(client_sock,buf,file_size,0); } //释放内容,这很重要 free(buf); close(fd); return; }
static krb5_error_code find_cred(krb5_context context, krb5_ccache id, krb5_principal server, krb5_creds **tgts, krb5_creds *out_creds) { krb5_error_code ret; krb5_creds mcreds; krb5_cc_clear_mcred(&mcreds); mcreds.server = server; ret = krb5_cc_retrieve_cred(context, id, KRB5_TC_DONT_MATCH_REALM, &mcreds, out_creds); if(ret == 0) return 0; while(tgts && *tgts){ if(krb5_compare_creds(context, KRB5_TC_DONT_MATCH_REALM, &mcreds, *tgts)){ ret = krb5_copy_creds_contents(context, *tgts, out_creds); return ret; } tgts++; } return not_found(context, server, KRB5_CC_NOTFOUND); }
B assoc (A a, list<std::pair<A, B>> l) { if (empty (l)) throw not_found (); std::pair<A, B> p=hd (l); if (p.first == a) return p.second; return assoc (a, tl (l)); }
static void *accept_request(void *arg) { char buf[1024]; char method [255]; char url[255]; char path[512]; /* int numchars; */ char *query_string = NULL; int cgi = 0; size_t i, j; struct stat st; int client = (int)(intptr_t)arg; /* numchars = get_line(client, buf, sizeof(buf)); */ get_line(client, buf, sizeof(buf)); i = 0; j = 0; while (!isspace(buf[j]) && (i < sizeof(method) - 1)) { method[i] = buf[j]; i++; j++; } method[i] = '\0'; i = 0; while ((isspace(buf[j])) && (j < sizeof(buf))) j++; while ((!isspace(buf[j])) && (i < sizeof(url) - 1) && (j < sizeof(buf))) { url[i] = buf[j]; i++; j++; } url[i] = '\0'; if (strcasecmp(method, "GET") == 0) { query_string = url; while ((*query_string != '?') && (*query_string != '\n')) query_string++; if (*query_string == '?') { cgi = 1; *query_string = '\0'; query_string++; } } puts(url); sprintf(path, "htdocs%s", url); if (path[strlen(path) - 1] == '/') strcat(path, "index.html"); if (stat(path, &st) == -1) { not_found(client); } else { if ((st.st_mode & S_IFMT) == S_IFDIR) strcat(path, "/index.html"); if (!cgi) serve_file(client, path); } close(client); return NULL; }
char *search_key(char *key, t_node *root) { int i; t_node *tmp; tmp = root; i = 0; while (key[i]) { if (tmp->kids[key[i] % 95] == NULL) return (not_found(key)); tmp = tmp->kids[key[i] % 95]; i++; } if (tmp->isleaf) return (ft_strdup(tmp->v)); return (not_found(key)); }
virtual particle_id_pair get_particle(particle_id_type const& id) const { typename particle_map::const_iterator i(particles_.find(id)); if (particles_.end() == i) { throw not_found(std::string("No such particle: id=") + boost::lexical_cast<std::string>(id)); } return *i; }
SpeciesType* Model::get_species_type_by_id(SpeciesTypeID const& id) const { species_type_map_type::const_iterator i(species_type_map_.find(id)); if (species_type_map_.end() == i) { throw not_found(boost::lexical_cast<std::string>(id)); } return (*i).second; }
B assoc (A a, std::list<std::pair<A, B>> const& l) { std::list<std::pair<A, B>>::const_iterator where= std::find_if (std::begin (l), std::end (l), [=] (std::pair<A, B> const& p) -> bool { return p.first == a; } ); if (where == std::end (l)) throw not_found (); return where->second; }
config config::operator[](const std::string& key) const { try { std::ostringstream os; os << path_ << "." << key; return config(json_[key], os.str()); } catch (const std::out_of_range& e) { throw not_found(path_, key); } catch (const std::bad_cast& e) { throw type_error(path_, pfi::text::json::json::Object, type()); } }
config config::operator[](const std::string& key) const { try { std::ostringstream os; os << path_ << "." << key; return config(json_[key], os.str()); } catch (const std::out_of_range& e) { throw JUBATUS_EXCEPTION(not_found(path_, key)); } catch (const std::bad_cast& e) { throw JUBATUS_EXCEPTION( type_error(path_, jubatus::util::text::json::json::Object, type())); } }
int memcache_delete (struct connection *c, const char *key, int key_len) { const int dog_len = get_at_prefix_length (key, key_len); key += dog_len; key_len -= dog_len; int counter_id; if (sscanf (key, "counter%d", &counter_id) >= 1) { delete_counter (counter_id, 0); write_out (&c->Out, "DELETED\r\n", 9); return 0; } return not_found (c); }
ENetStorageRemoveResult SNetStorageByKeyRPC::Remove(const string& key, TNetStorageFlags flags) { m_NetStorageRPC->m_UseNextSubHitID.ProperCommand(); CJsonNode request(m_NetStorageRPC->MkObjectRequest("DELETE", key, flags)); CJsonNode response( m_NetStorageRPC->Exchange(m_NetStorageRPC->m_Service, request)); CJsonNode not_found(response.GetByKeyOrNull("NotFound")); return not_found && not_found.AsBoolean() ? eNSTRR_NotFound : eNSTRR_Removed; }
static void serve_file(int client, const char *filename) { FILE *resource = NULL; /* int numchars = 1; */ /* char buf[1024]; */ resource = fopen(filename, "r"); if (resource == NULL) { not_found(client); } else { headers(client, filename); cat(client, resource); } fclose(resource); }
void serve_file(nunetwork_socket client, char *filename) { FILE *resource = NULL; char buf[1024]; buf[0] = 'A'; buf[1] = '\0'; resource = fopen(filename, "r"); if (resource == NULL) not_found(client); else { headers(client, filename); binarycat(client, resource); } fclose(resource); }