void* pt_download(void* args) { struct pt_dwdata* pdp = (struct pt_dwdata*)args; //printf("thread: %d, offset: %d, length:%d\n", pdp->number, pdp->offset, pdp->length); int64 total = 0; int trying = 0; char buf[1024]; char localname[256]; sprintf(localname, ".tmp/%s_%d", g_pDM->getBaseName(), pdp->number); int64 dataLeft = pdp->length - total; FILE* fp = fopen(localname, "wb"); char tmp[1024]; int sock = -1; while (total < pdp->length) { sock = sock_open(g_pDM->getHost(), g_pDM->getPort()); if (sock <= 0) { printf(RED"Error: "WHITE"connect server failed, retry: %d times\n", trying++); sleep(5); continue; } //welcome message FILE* fpSock = fdopen(sock, "r"); setbuf(fpSock, (char *)0); fgets(buf, sizeof(buf), fpSock); printf("thread: "YELLOW"%d"WHITE", connect server "GREEN"OK\n"WHITE, pdp->number); dataLeft = pdp->length - total; sprintf(tmp, "get %s %llu %llu", g_pDM->getFileName(), pdp->offset + total, dataLeft); printf("%s\n", tmp); sock_cmd(sock, tmp); //printf("get %s %d %d\n", g_pDM->getFileName(), pdp->offset + total, dataLeft); int64 len; while (dataLeft)// < pdp->length) { len = sizeof(buf) > dataLeft ? dataLeft : sizeof(buf); //printf("%d\n", len); int n = sock_read(sock, buf, (int)len); if (n <= 0) { printf("sock read return %d bytes\n", n); break; } fwrite(buf, n, 1, fp); dataLeft -= n; total += n; g_pDM->setDwSize(pdp->number, total); } close(sock); //printf("%d/%d\n", total, g_data.final_size); } fclose(fp); printf("thread: "YELLOW"%d"WHITE", my task is "GREEN"OK\n"WHITE, pdp->number); return NULL; }
/** * http_handler - http handler */ void http_handler(struct evhttp_request *req, void *arg) { time_t timep; struct tm *m; struct stat info; struct evbuffer *buf; buf = evbuffer_new(); // 分析URL参数 char *decode_uri = strdup((char*) evhttp_request_uri(req)); decode_uri = evhttp_decode_uri(decode_uri); if (strcmp(decode_uri, "/") == 0) decode_uri = "/index.html"; sprintf(http_req_line.request_uri, "%s", decode_uri); // 返回给浏览器的头信息 evhttp_add_header(req->output_headers, "Server", "logadmin"); evhttp_add_header(req->output_headers, "Connection", "close"); // 取得请求时间 time(&timep); m = localtime(&timep); sprintf(http_req_line.request_time, "%4d-%02d-%02d %02d:%02d:%02d", (1900+m->tm_year), (1+m->tm_mon), m->tm_mday, m->tm_hour, m->tm_min, m->tm_sec); // 获取ACTION的值 struct evkeyvalq params; evhttp_parse_query(decode_uri, ¶ms); char *action = (char*)evhttp_find_header(¶ms, "action"); // 处理不同的ACTION if (action) { char *tmp = ""; evhttp_add_header(req->output_headers, "Content-Type", "text/html; charset=UTF-8"); if (strcmp(action, "loginfo") == 0) { char *loghost = (char*)evhttp_find_header(¶ms, "loghost"); char *logport = (char*)evhttp_find_header(¶ms, "logport"); char *logname = (char*)evhttp_find_header(¶ms, "logname"); char *pagenum = (char*)evhttp_find_header(¶ms, "pagenum"); int pnum = pagenum ? atoi(pagenum) : 1; int port = logport ? atoi(logport) : 8706; if (logname) { int psize = 20; char *logreg = (char*)evhttp_find_header(¶ms, "logreg"); if(!logreg || !strlen(logreg)){ logreg = ".*"; } char *cmd = joinstr("grep '%s' %s -c && grep '%s' %s | tail -n +%d | head -n %d", logreg, logname, logreg, logname, (pnum - 1) * psize, psize); if (!loghost || !strlen(loghost)) { tmp = exec_cmd(cmd); } else { sock_cmd(loghost, port, cmd, (char*)&tmp, MEM_SIZE); } } evbuffer_add_printf(buf, "%s", tmp); evhttp_send_reply(req, HTTP_OK, "OK", buf); } else if(strcmp(action, "loglist") == 0) { char *loghost = (char*)evhttp_find_header(¶ms, "loghost"); char *logport = (char*)evhttp_find_header(¶ms, "logport"); char *dirname = (char*)evhttp_find_header(¶ms, "dirname"); int port = logport ? atoi(logport) : 8706; if (dirname) { char *cmd = joinstr("ls -lF %s | awk '{print $9}'", dirname); if (!loghost || !strlen(loghost)) { tmp = exec_cmd(cmd); } else { sock_cmd(loghost, port, cmd, (char*)&tmp, MEM_SIZE); } } evbuffer_add_printf(buf, "%s", tmp); evhttp_send_reply(req, HTTP_OK, "OK", buf); } else if(strcmp(action, "logconf") == 0) { #ifndef __WIN32 GKeyFile* config = g_key_file_new(); GKeyFileFlags flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS; if (g_key_file_load_from_file(config, "./conf/logadmin.conf", flags, NULL)) { int i; gsize length; gchar** groups = g_key_file_get_groups(config, &length); for (i = 0; i < (int)length; i++) { gchar* host = g_key_file_get_string(config, groups[i], "host", NULL); gchar* path = g_key_file_get_string(config, groups[i], "path", NULL); gchar* port = g_key_file_get_string(config, groups[i], "port", NULL); if(!port || !strlen(port)) port = "8706"; if (host && path) tmp = joinstr("%s%s:%s;%s\n", tmp, host, port, path); } } g_key_file_free(config); #endif evbuffer_add_printf(buf, "%s", tmp); evhttp_send_reply(req, HTTP_OK, "OK", buf); } else { evbuffer_add_printf(buf, "<html><head><title>870 Error Action</title></head><body><h1 align=\"center\">870 Error Action</h1></body></html>"); evhttp_send_reply(req, 870, "Error Action", buf); } evbuffer_free(buf); return; } // 获取请求的服务器文件路径 char *dir_root = getcwd(NULL, 0); char *real_path = joinstr("%s/%s%s", dir_root, DOCUMENT_ROOT, http_req_line.request_uri); if (stat(real_path,&info) == -1) { evhttp_add_header(req->output_headers, "Content-Type", "text/html; charset=UTF-8"); if (errno == ENOENT) { evbuffer_add_printf(buf, "<html><head><title>404 Not Found</title></head><body><h1 align=\"center\">404 Not Found</h1></body></html>"); evhttp_send_reply(req, 404, "Not Found", buf); } else if(access(real_path,R_OK) < 0) { evbuffer_add_printf(buf, "<html><head><title>403 Not Found</title></head><body><h1 align=\"center\">403 Forbidden</h1></body></html>"); evhttp_send_reply(req, 403, "Forbidden", buf); } else { evbuffer_add_printf(buf, "<html><head><title>500 Server Error</title></head><body><h1 align=\"center\">500 Server Error</h1></body></html>"); evhttp_send_reply(req, 500, "Server Error", buf); } } else if(S_ISREG(info.st_mode)) { // 设置HEADER头并输出内容到浏览器 evhttp_add_header(req->output_headers, "Content-Type", joinstr("%s; charset=UTF-8", mime_type(real_path))); evbuffer_add_printf(buf, "%s", read_log(real_path)); evhttp_send_reply(req, HTTP_OK, "OK", buf); } // 内存释放 evbuffer_free(buf); free(dir_root); }
int64 DwManager::getSizeFromServer() { if (m_fd != -1) { close(m_fd); m_fd = -1; } int sock = sock_open(m_host, m_port); if (sock <= 0) { printf("connect server failed\n"); exit (1); } FILE* fpSock = fdopen(sock, "r"); if (fpSock == NULL) { close(sock); return -2; } setbuf(fpSock, (char *)0); char buf[1024]; if (fgets(buf, sizeof(buf), fpSock) == NULL) { close(sock); return -3; } printf("<=%s", buf); printf(GREEN"get %s\n"WHITE, m_filename); sock_cmd(sock, "size %s", m_filename); if (fgets(buf, sizeof(buf), fpSock) == NULL) { close(sock); return -4; } //if (strncmp(buf, "OK", 2) != 0) if (strncmp(buf, "+OK", 3) != 0) // simon { printf(RED"Error: %s", buf); close(sock); return -5; } // printf(YELLOW"file size: "WHITE"%s", buf + 3); printf(YELLOW"file size: "WHITE"%s", buf + 4); // simon // int64 total = atoll(buf + 3); int64 total = atoll(buf + 4); // simon if (total <= 0) { printf(RED"Error: "YELLOW"Wrong size:"WHITE"%d\n", total); close(sock); return -6; } //printf("file size: %llu\n", total); //close(sock); m_fd = sock; m_filesize = total; return m_filesize; }