/* * Send an HTTP authorization failed response. */ void http_response_send_basic_auth(struct http_response *resp, const char *realm) { http_response_set_header(resp, 0, HTTP_HEADER_WWW_AUTHENTICATE, "Basic realm=\"%s\"", realm); _http_head_remove(resp->msg->head, HTTP_HEADER_LAST_MODIFIED); http_response_send_error(resp, HTTP_STATUS_UNAUTHORIZED, NULL); }
char * http_servlet_tmpl_func_redirect(struct tmpl_ctx *ctx, char **errmsgp, int ac, char **av) { struct http_servlet_tmpl_arg *const arg = tmpl_ctx_get_arg(ctx); const char *const mtype = tmpl_ctx_get_mtype(ctx); if (ac != 2) { errno = EINVAL; return (NULL); } if (http_response_set_header(arg->resp, 0, HTTP_HEADER_LOCATION, "%s", av[1]) == -1) return (NULL); http_response_send_error(arg->resp, HTTP_STATUS_FOUND, NULL); return (STRDUP(mtype, "")); }
/* * Convert 'errno' into an HTTP error response. */ void http_response_send_errno_error(struct http_response *resp) { int code; switch (errno) { case ENOENT: case ENOTDIR: code = HTTP_STATUS_NOT_FOUND; break; case EPERM: case EACCES: code = HTTP_STATUS_FORBIDDEN; break; default: code = HTTP_STATUS_INTERNAL_SERVER_ERROR; break; } http_response_send_error(resp, code, "%s", strerror(errno)); }
int http_response_create(struct server_conf *server,http_request_line_t *request,http_response_t *response){ char path[MAX_FILENAME_LEN]; char buf[BUFF_SIZE]; char *ls=NULL,*content=NULL; int response_len; if(server&&server->docroot) strncpy(path,server->docroot,strlen(server->docroot)+1); else strncpy(path,ROOTDIR,strlen(ROOTDIR)+1); if(request->uri_start==request->uri_end){ //if(request->uri_end=request->uri_start){ ls=http_response_ls(path); if(ls){ //printf("ls len is %d\n",strlen(ls)); http_response_add_status_line(response,200,desc_200); sprintf(buf,"Content-length:%d",strlen(ls)); http_response_add_headers(response,buf); con_close(response); http_response_add_body(response,ls); //printf("response: %d\n%s\n",response->len,response->data); //printf("header length is %d\n",strlen(buf)+17); write(request->fd,response->data,response->len); free(ls); free_response(response); return 1; }//endif else{ log_debug(LOG_LEVEL_DEBUG,"open for document root dir failed!"); http_response_send_error(request->fd,500,desc_500,content_500); return(-1); }//end else }//endif uri is "/",do ls -la else{ /*main work*/ printf("path:%s\n",path); strncat(path,request->uri_start,request->uri_end-request->uri_start+1); printf("path:%s\n",path); content=http_response_readfile(path); if(!content){ //404 http_response_send_error(request->fd,404,desc_404,content_404); log_debug(LOG_LEVEL_DEBUG,"send not found!"); return 1; } //printf("content is 0x%x\n",content); http_response_add_status_line(response,200,desc_200); response_len=strlen(content); sprintf(buf,"Content-length:%d",response_len); http_response_add_headers(response,buf); con_close(response); //printf("response:%s\n",content); http_response_add_body(response,content); //printf("content ok\n"); write(request->fd,response->data,response->len); //printf("content len is %d\n",strlen(content)); //printf("free content(0x%x)\n",content); free(content); content=NULL; free_response(response); return 1; }//end else }
static int WebServletRun(struct http_servlet *servlet, struct http_request *req, struct http_response *resp) { FILE *f; const char *path; const char *query; int priv = 0; if (Enabled(&gWeb.options, WEB_AUTH)) { const char *username; const char *password; ConsoleUser u; struct console_user iu; /* Get username and password */ if ((username = http_request_get_username(req)) == NULL) username = ""; if ((password = http_request_get_password(req)) == NULL) password = ""; strlcpy(iu.username, username, sizeof(iu.username)); RWLOCK_RDLOCK(gUsersLock); u = ghash_get(gUsers, &iu); RWLOCK_UNLOCK(gUsersLock); if ((u == NULL) || strcmp(u->password, password)) { http_response_send_basic_auth(resp, "Access Restricted"); return (1); } priv = u->priv; } if (!(f = http_response_get_output(resp, 1))) { return 0; } if (!(path = http_request_get_path(req))) return 0; if (!(query = http_request_get_query_string(req))) return 0; if (!strcmp(path,"/mpd.css")) { http_response_set_header(resp, 0, "Content-Type", "text/css"); WebShowCSS(f); } else if (!strcmp(path,"/bincmd")) { http_response_set_header(resp, 0, "Content-Type", "text/plain"); http_response_set_header(resp, 1, "Pragma", "no-cache"); http_response_set_header(resp, 1, "Cache-Control", "no-cache, must-revalidate"); pthread_cleanup_push(WebServletRunCleanup, NULL); GIANT_MUTEX_LOCK(); WebRunBinCmd(f, query, priv); GIANT_MUTEX_UNLOCK(); pthread_cleanup_pop(0); } else if (!strcmp(path,"/") || !strcmp(path,"/cmd")) { http_response_set_header(resp, 0, "Content-Type", "text/html"); http_response_set_header(resp, 1, "Pragma", "no-cache"); http_response_set_header(resp, 1, "Cache-Control", "no-cache, must-revalidate"); pthread_cleanup_push(WebServletRunCleanup, NULL); GIANT_MUTEX_LOCK(); fprintf(f, "<!DOCTYPE HTML " "PUBLIC \"-//W3C//DTD HTML 4.01//EN\" " "\"http://www.w3.org/TR/html4/strict.dtd\">\n"); fprintf(f, "<HTML>\n"); fprintf(f, "<HEAD><TITLE>Multi-link PPP Daemon for FreeBSD (mpd)</TITLE>\n"); fprintf(f, "<LINK rel='stylesheet' href='/mpd.css' type='text/css'>\n"); fprintf(f, "</HEAD>\n<BODY>\n"); fprintf(f, "<H1>Multi-link PPP Daemon for FreeBSD</H1>\n"); if (!strcmp(path,"/")) WebShowSummary(f, priv); else if (!strcmp(path,"/cmd")) WebRunCmd(f, query, priv); GIANT_MUTEX_UNLOCK(); pthread_cleanup_pop(0); fprintf(f, "</BODY>\n</HTML>\n"); } else { http_response_send_error(resp, 404, NULL); } return 1; }