void cb (ptr<sha_query_res_t> res, clnt_stat err) { if (err) error_page (strbuf () << err); else if (res->status == ADB_NOT_FOUND) error_page ("Word not found in dictionary!"); else if (res->status != ADB_OK) error_page ("Database errorr encountered!"); else success_page (*res->res); }
void process () { if (cgi.blookup ("z")) { str x = cgi["x"]; if (!x) error_page ("Error: no input given!"); else { ptr<sha_query_res_t> res = New refcounted<sha_query_res_t> (); ok_sha->shadb->call (SHA_QUERY, &x, res, wrap (this, &okclnt_sha_t::cb, res)); } } else { output_page (); } }
int main(void) { struct sockaddr_in sin, cin; socklen_t len = sizeof(cin); int lfd, cfd, fd; pid_t pid; int sock_opt = 1; int port; char path[MAX_LINE]; struct stat statbuf; signal(SIGCHLD,SIG_IGN); signal(SIGPIPE,SIG_IGN); printf("initializing...\n"); if(init(&sin, &lfd, &port,path) == -1){ DEBUG_PRINT("error during initializing\n"); exit(1); } while(1){ DEBUG_PRINT("wating connection..\n"); cfd=accept(lfd, (struct sockaddr *) &cin, &len); if(cfd == -1){ perror("fail to accept\n"); exit(1); } pid=fork(); if(pid < 0){ perror("fail to fork\n"); exit(1); }else if(pid == 0){ close(lfd); // close the listening fd for the child // process if((get_path(cfd, path)) == -1){ DEBUG_PRINT("error during getting filepath\n"); exit(1); } if((fd=open(path, O_RDONLY)) == -1){ perror("fail to open the request file\n"); exit(1); } if((fstat(fd, &statbuf)) == -1){ perror("fail to get file status\n"); exit(1); } if(!S_ISREG(statbuf.st_mode)){ printf("not a regular file!\n");//enter here if(error_page(cfd) == -1){ DEBUG_PRINT("error during writing error-page\n"); close(cfd); exit(1); } close(cfd); exit(0); } if(statbuf.st_mode & S_IXOTH){ dup2(cfd, STDOUT_FILENO); if(execl(path, path, NULL) == -1){ perror("fail to exec\n"); exit(1); } } if(fd < 0){ if(error_page(cfd) == -1){ DEBUG_PRINT("error during writing error page\n"); close(cfd); exit(0); } } printf("%s\n",path); if((write_page(cfd, fd, path)) == -1){ DEBUG_PRINT("error during writing page\n"); exit(1); } close(fd); close(cfd); exit(0); }else // above is child process close(fd); } return 0; }
int main(void) { struct sockaddr_in sin, cin; socklen_t len = sizeof(cin); int lfd, cfd, fd; pid_t pid; int sock_opt = 1; char path[MAX_LINE]; struct stat statbuf; char log_msg[CM_MAX_LOG_MSG_LEN]; signal(SIGCHLD, SIG_IGN); signal(SIGPIPE, SIG_IGN); if (-1 == log_init("webserver", get_current_dir_name())) { DEBUG_PRINT("Error during log init\n"); exit(1); } CM_RUN_LOG("webserver", INFO, "Log initialization succeed."); if (-1 == init(&sin, &lfd, path)) { DEBUG_PRINT("error during initialization\n"); exit(1); } CM_RUN_LOG("webserver", INFO, "Webserver service initialization succeed."); while (1) { DEBUG_PRINT ("waiting connection...\n"); cfd = accept(lfd, (struct sockaddr *)&cin, &len); if (-1 == cfd) { perror("fail to accept"); exit(1); } pid = fork(); if (pid < 0) { perror("fail to fork"); exit(1); } else if (0 == pid) { close(lfd); if (-1 == get_path(cfd, path)) { DEBUG_PRINT ("error during geting filepath\n"); exit(1); } if (stat("/home/tom/index.html", &statbuf) < 0) { perror("fail to get file status\n"); exit(1); } if (!S_ISREG(statbuf.st_mode)) { if (-1 == error_page(cfd)) { DEBUG_PRINT("error during writing error-page\n"); close(cfd); exit(1); } close(cfd); exit(0); } /* If it's a exutable file, execute it and send the result to client. */ if (statbuf.st_mode & S_IXOTH) { dup2(cfd, STDOUT_FILENO); if (-1 == execl(path, path, NULL)) { perror("fail to exec"); exit(1); } } if ((fd = open(path, O_RDONLY)) < 0) { if (-1 == error_page(cfd)) { DEBUG_PRINT("error during writing error-page\n"); close(cfd); exit(1); } close(cfd); exit(0); } if (-1 == write_page(cfd, path, fd)) { DEBUG_PRINT("error during writing page\n"); exit(1); } close(fd); close(cfd); exit(0); } else { close(cfd); } } return 0; }
int main(int argc, char const *argv[]) { list_t * colaboradores = NULL; list_t * retiradas = NULL; list_t * relatorio = list_blank_list(); node_t * node = NULL; colaborador_t * colaborador = NULL; retirada_t * retirada = NULL; char buffer[500]; unsigned int colaborador_id = 0; char * tipo_relatorio = NULL; request_t * request = NULL; response_t * response = response_empty(NULL); var_t * var = NULL; error_t error = ERROR_NULL; unsigned int user_id; unsigned char user_level; request_process(&request); login_info(request, &user_id, &user_level); if (user_level > ANONYMOUS) { node = request->GET->first; while (node != NULL) { var = (var_t *) node->data; if (strcmp(var->name, "por") == 0) { tipo_relatorio = var->value; } if (strcmp(var->name, "colaboradorid") == 0) { colaborador_id = atoi(var->value); } node = node->next; } colaboradores_load(&colaboradores, NULL); retiradas_load(&retiradas, NULL); if (strcmp(tipo_relatorio, "item") == 0) { process_report(&relatorio, retiradas, 0); login_refresh_session(&response, user_id, user_level); response_write_template(&response, "templates/header.html"); response_write_template(&response, "templates/relatorios_item.html"); if (relatorio->first == NULL) { response_write(&response, "<p>Não existe informações para serem exibidas</p>"); } else { response_write(&response, "<table class=\"table table-bordered table-striped\">" "<tr><th>Item</th><th>Retirado por</th><th>Data da última retirada</th>" "<th>Quantidade restante</th></tr>"); node = relatorio->first; while (node != NULL) { retirada = (retirada_t *) node->data; sprintf(buffer, "<tr><td>%s</td><td>%s</td><td>%s</td><td class=\"quantidade\">%u</td></tr>", retirada->item->nome, retirada->colaborador->nome, retirada->data, retirada->item->quantidade); response_write(&response, buffer); retirada_free(&retirada); node = node->next; } response_write(&response, "</table>"); } response_write(&response, "<script src=\"/js/relatorios.js\"></script>"); response_write_template(&response, "templates/footer.html"); } else if (strcmp(tipo_relatorio, "colaborador") == 0) { if (colaborador_id > 0) { process_report(&relatorio, retiradas, colaborador_id); } login_refresh_session(&response, user_id, user_level); response_write_template(&response, "templates/header.html"); response_write_template(&response, "templates/relatorios_colaborador.html"); if (colaborador_id > 0) { if (relatorio->first == NULL) { response_write(&response, "<p>Não existe informações para serem exibidas</p>"); } else { response_write(&response, "<table class=\"table table-bordered table-striped\">" "<tr><th>Item</th><th>Retirado por</th><th>Data da última retirada</th>" "<th>Quantidade restante</th></tr>"); node = relatorio->first; while (node != NULL) { retirada = (retirada_t *) node->data; sprintf(buffer, "<tr><td>%s</td><td>%s</td><td>%s</td><td class=\"quantidade\">%u</td></tr>", retirada->item->nome, retirada->colaborador->nome, retirada->data, retirada->item->quantidade); response_write(&response, buffer); retirada_free(&retirada); node = node->next; } response_write(&response, "</table>"); } } else { response_write(&response, "<p id=\"message\">Para ver o relatório selecione um colaborador acima e aperte \"Ver relatório\"</p>"); } response_write(&response, "<script>var colaboradores = ["); node = colaboradores->first; while (node != NULL) { colaborador = (colaborador_t *) node->data; sprintf(buffer, "{ id: %u, nome: \"%s\" },", colaborador->id, colaborador->nome); response_write(&response, buffer); colaborador_free(&colaborador); node = node->next; } response_write(&response, "];</script>"); response_write(&response, "<script src=\"/js/relatorios.js\"></script>"); response_write(&response, "<script src=\"/js/relatorios_colaborador.js\"></script>"); response_write_template(&response, "templates/footer.html"); } else { error = ERROR_RELATORIO_INVALIDO; } if (error != ERROR_NULL) { if (error == ERROR_RELATORIO_INVALIDO) { sprintf(buffer, "/cgi-bin/relatorios?por=item"); } else { sprintf(buffer, "/cgi-bin/relatorios?por=%s", tipo_relatorio); } error_page(&response, error, buffer); } } else { error_page(&response, ERROR_LOGIN_REQUIRED, "/"); } response_send(response); request_free(request); return 0; }
void add_prompt(struct handler_args* hargs, struct table_action* t_action, struct prompt_rule* rule, struct pgtype_datum* pgtype, char* options[], int row_index, xmlNodePtr child_of, xmlChar* fname, xmlChar* fvalue){ // Initialize a struct prompt_add_args static char* default_name = "default"; static char* input_text = "input_text"; struct prompt_add_args arg_record = (struct prompt_add_args){ .hargs = hargs, .t_action = t_action, .rule = rule, .pgtype = pgtype, .options = options, .row_index = row_index, .child_of = child_of, .fname = fname, .fvalue = fvalue }; struct prompt_add_args* args = &arg_record; struct prompt_rule default_rule = (struct prompt_rule){ .form_name = default_name, .fieldname = fname, .prompt_type = "input_text", }; if (args->rule == NULL) args->rule = &default_rule; if (args->rule->prompt_type == NULL) args->rule->prompt_type = input_text; xmlNodePtr input = NULL; // !*&^$#%!!! // struct prompt_adder* prompt = xmlHashLookup(prompt_type_hash, args->rule->prompt_type); if (prompt == NULL){ fprintf(hargs->log, "%f %d %s:%d fail prompt type not found %s\n", gettime(), hargs->request_id, __func__, __LINE__, args->rule->prompt_type); error_page(hargs, SC_INTERNAL_SERVER_ERROR, "Prompt Type not found"); return; } int pk; bool is_pkey = false; char* mod_fname; asprintf(&mod_fname, "%s", fname); int k; for(k=0; k<strlen(mod_fname); k++){ if (mod_fname[k] == '[') mod_fname[k] = '\0'; } if (t_action != NULL){ for (pk=0; pk<t_action->nbr_pkeys; pk++){ if (strcmp(mod_fname, t_action->pkeys[pk]) == 0){ is_pkey = true; break; } } } free(mod_fname); mod_fname = NULL; xmlNodePtr fieldset = NULL; if ((t_action != NULL) && (t_action->prompt_container != NULL) && ( strcmp(t_action->prompt_container, "fieldset") == 0)){ // Everything except a hidden field gets a fieldset. // XXXXX Make fieldset a field in prompt_rule so that // XXXXX multiple fields can exist within one fieldset. // XXXXX The managing of fieldsets would have to happen // XXXXX further up the call stack, otherwise add_prompts // XXXXX would have to keep state to know about prior fieldsets. if ((args->rule != NULL) && (args->rule->prompt_type != NULL) && strncmp("input_hidden", args->rule->prompt_type, strlen("input_hidden") ) != 0){ fieldset = xmlNewChild(child_of, NULL, "fieldset", NULL); xmlNewTextChild(fieldset, NULL, "legend", fname ); args->child_of = fieldset; } } // !*&^$%#!!! // Execute the prompt adder function for the given prompt type. input = prompt->add_prompt(args); if (input == NULL) return; // Some error occured if (t_action != NULL){ fprintf(hargs->log, "%f %d %s:%d t_action->add_description=%s\n", gettime(), hargs->request_id, __func__, __LINE__, (t_action->add_description) ? "true":"false"); } // Add the description off to the side if it is available. if ( (t_action != NULL) && (t_action->add_description) && (args->pgtype != NULL) && (args->pgtype->description != NULL) && (args->pgtype->description[0] != '\0') && (fieldset != NULL)){ xmlNodePtr description; description = xmlNewTextChild(fieldset, NULL, "span", args->pgtype->description); xmlNewProp(description, "class", "description"); } if ((args->rule->readonly) || (is_pkey && (fvalue!=NULL))){ xmlNewProp(input,"readonly","readonly"); } return; } void init_prompt_type_hash(void){ static struct prompt_adder const static_prompt_types[] = { { .prompt_type = button, .name = "button", .add_prompt = add_button }, { .prompt_type = input_checkbox, .name = "input_checkbox", .add_prompt = add_input_checkbox }, { .prompt_type = input_file,
/* Calls the function that prints the PiCO QL error page (.html). */ void print_pico_ql_error_page(FILE *f) { error_page(f); }
int main(int argc, char const *argv[]) { list_t * list = NULL; node_t * node = NULL; item_t * item = NULL; item_t * novo_item = NULL; char buffer[500]; request_t * request = NULL; response_t * response = response_empty(NULL); var_t * var = NULL; char * query = NULL; error_t error = ERROR_NULL; unsigned int user_id; unsigned char user_level; request_process(&request); login_info(request, &user_id, &user_level); if (user_level > ANONYMOUS) { node = request->GET->first; while (node != NULL) { var = (var_t *) node->data; if (strcmp(var->name, "q") == 0) { query = var->value; } node = node->next; } itens_load(&list, query); if (list->last != NULL) { item = (item_t *) list->last->data; last_id = item->id; } if (request->method == POST) { novo_item = form_process(request); if (novo_item != NULL) { if (novo_item->id == 0) { novo_item->id = ++last_id; list_add(list, novo_item); } else { node = list->first; while (node != NULL) { item = (item_t *) node->data; if (item->id == novo_item->id) { item->quantidade = novo_item->quantidade; item->fim_estoque = novo_item->fim_estoque; break; } node = node->next; } } itens_save(list); } else { error = ERROR_ITEM; } } if (error != ERROR_NULL) { error_page(&response, error, "/cgi-bin/itens"); } else { login_refresh_session(&response, user_id, user_level); response_write_template(&response, "templates/header.html"); response_write_template(&response, "templates/itens.html"); if (list->first == NULL) { response_write(&response, "<p>Nenhum item encontrado</p>"); } else { response_write(&response, "<table class=\"table table-bordered table-striped\">" "<tr><th>Id</th><th>Nome</th><th>Tipo</th><th>Descrição</th>" "<th>Quantidade</th><th>Previsão de fim de estoque</th>" "<th class=\"edit\">Ações</th></tr>"); node = list->first; while (node != NULL) { item = (item_t *) node->data; sprintf(buffer, "<tr><td>%u</td><td class=\"nome\">%s</td><td class=\"tipo\">%s</td>" "<td class=\"descricao\">%s</td><td class=\"quantidade\">%u</td>" "<td class=\"fimestoque\">%s</td><td class=\"edit\">" "<div class=\"btn-group\">" "<button class=\"btn edit-btn\" data-id=\"%u\"><i class=\"icon-pencil\"></i></button>" "</div>" "</td></tr>", item->id, item->nome, item->tipo, item->descricao, item->quantidade, item->fim_estoque, item->id); response_write(&response, buffer); item_free(&item); node = node->next; } response_write(&response, "</table>"); } response_write(&response, "<script src=\"/js/itens.js\"></script>"); response_write_template(&response, "templates/footer.html"); } } else { error_page(&response, ERROR_LOGIN_REQUIRED, "/"); } response_send(response); request_free(request); return 0; }