📄 main.c
字号:
#include "wput.h"#if !defined _WIN32static jmp_buf env_alrm;#endifintmain(int argc, char **argv) { char *s, *p; int i, j; char buff[1024]; char prm[1024]; char cmd[1048]; char *add_headers; int max, min; /* Set default parameters */ url = URL; h_char = HOST; h_flag = PORT; v_char = PROTOCOL; p_char = PROXY_HOST; p_flag = PROXY_PORT;#if !defined _WIN32 t_flag = TIMEOUT;#endif i_flag = 0; min = max = 0; a_char = HEADERS; add_headers = strdup(a_char); /* find newlines */ if (a_char != "") { a_char = fnlines(HEADERS); }#if !defined _WIN32 /* Define signal trap */ (void)signal(SIGALRM, catchalarm); /* Jump here if timeout occurs */ sigsetjmp(env_alrm, 1);#endif /* Enter main loop */ printp(); while (fgets(buff, 1024, stdin)) { /* delete new-line: */ buff[strlen(buff)-1] = '\0'; /* skip leading white space: */ for (i = 0; isspace(buff[i]) ; i++) ; /* empty command or comment: */ if (!strlen(&buff[i]) || !strncmp("#", &buff[i], 1)) { printp(); continue; } /* get parameters */ for (j = i; !isspace(buff[j]) && buff[j] != '\0'; j++) ; for (; isspace(buff[j]) ; j++) ; /* empty parameter list? */ if (!strlen(&buff[j])) j = i; /* built-in commands: */ /* exit */ if (!iscmd("x", &buff[i], 1)) { exit (0); /* help */ } else if (!iscmd("?", &buff[i], 1)) { printf("WPUT version %s\n", VERSION); printf("Available commands are:\n" " ? show this text\n" " sh show parameters\n" " q return only HTTP status codes\n" " qq be quiet\n" " nq be noisy (default)\n" " h [host:port] change host and port\n" " p [host:port] use proxy server host:port\n" " r [port] change source port\n" " u [url] change base url\n" " v [string] change HTTP-version (1.0)\n" " a [string] additional HTTP-headers\n" " i [secs] idle time before sending data\n"#if !defined _WIN32 " t [secs] network timeout\n"#endif " s [string] send string\n" " x exit wput\n"); /* be quiet */ } else if (!iscmd("q", &buff[i], 1)) { q_flag = 1; /* be quiet */ } else if (!iscmd("qq", &buff[i], 2)) { q_flag = 2; /* be noisy */ } else if (!iscmd("nq", &buff[i], 2)) { q_flag = 0; /* show parameters */ } else if (!iscmd("sh", &buff[i], 2)) { printf("Current parameters:\n"); if (h_flag == 80) printf(" host: %s\n", h_char); else printf(" host: %s:%d\n", h_char, h_flag); if (p_char != "") printf(" proxy: %s:%d\n", p_char, p_flag); if (!r_flag) printf(" source port: any\n"); else printf(" source port: %d\n", r_flag); printf(" base url: %s\n", url); printf(" HTTP version: %s\n", v_char); printf(" additional headers: %s\n", add_headers); if (min && max) printf(" idle time: %d-%d secs\n", min, max); else printf(" idle time: %d secs\n", i_flag);#if !defined _WIN32 printf(" timeout: %d secs\n", t_flag);#endif printf(" quiet: %s\n", q_flag ? "yes" : "no"); /* idle time */ } else if (!iscmd("i", &buff[i], 1)) { /* No parameters */ if (j == i) { printf("seconds: "); fgets(prm, 1024, stdin); prm[strlen(prm)-1] = '\0'; if (!strlen(prm)) { printp(); continue; } p = prm; } else p = &buff[j]; if (sscanf(p, "%d-%d", &min, &max) != 2) { min = 0; max = 0; i_flag = atoi(p); } else if (min > max) { i_flag = min; min = max; max = i_flag; } else if (min == max) { i_flag = min; min = 0; max = 0; } else if (min < 0 || max < 0) { i_flag = 0; min = 0; max = 0; }#if !defined _WIN32 /* change timeout */ } else if (!iscmd("t", &buff[i], 1)) { /* No parameters */ if (j == i) { printf("new port: "); fgets(prm, 1024, stdin); prm[strlen(prm)-1] = '\0'; if (!strlen(prm)) { printp(); continue; } t_flag = atoi(prm); } else t_flag = atoi(&buff[j]);#endif /* change proxy */ } else if (!iscmd("p", &buff[i], 1)) { /* No parameters */ if (j == i) { printf("new proxy (host:port): "); fgets(prm, 1024, stdin); prm[strlen(prm)-1] = '\0'; if (!strlen(prm)) { p_char = ""; printp(); continue; } else p = prm; } else p = &buff[j]; s = p; while (*p++ != '\0') if (*p == ':') { *p = '\0'; p_flag = atoi(p + 1); } else if (*p == ' ') { *p = '\0'; break; } p_char = strdup(s); /* change source port */ } else if (!iscmd("r", &buff[i], 1)) { /* No parameters */ if (j == i) { printf("new source port: "); fgets(prm, 1024, stdin); prm[strlen(prm)-1] = '\0'; if (!strlen(prm)) { printp(); continue; } r_flag = atoi(prm); } else r_flag = atoi(&buff[j]); /* change hostname */ } else if (!iscmd("h", &buff[i], 1)) { /* No parameters */ if (j == i) { printf("new host (host:port): "); fgets(prm, 1024, stdin); prm[strlen(prm)-1] = '\0'; if (!strlen(prm)) { printp(); continue; } p = prm; } else p = &buff[j]; s = p; while (*p++ != '\0') if (*p == ':') { *p = '\0'; h_flag = atoi(p + 1); } else if (*p == ' ') { *p = '\0'; break; } h_char = strdup(s); /* change url */ } else if (!iscmd("u", &buff[i], 1)) { /* No parameters */ if (j == i) { printf("new base url: "); fgets(prm, 1024, stdin); prm[strlen(prm)-1] = '\0'; if (!strlen(prm)) url = ""; else url = strdup(prm); } else url = strdup(&buff[j]); /* change HTTP version */ } else if (!iscmd("v", &buff[i], 1)) { /* No parameters */ if (j == i) { printf("HTTP version: "); fgets(prm, 1024, stdin); prm[strlen(prm)-1] = '\0'; if (!strlen(prm)) { printp(); continue; } p = prm; } else p = &buff[j]; v_char = strdup(p); /* change headers */ } else if (!iscmd("a", &buff[i], 1)) { /* No parameters */ if (j == i) { printf("add headers: "); fgets(prm, 1024, stdin); prm[strlen(prm)-1] = '\0'; if (!strlen(prm)) { a_char = ""; add_headers = strdup(a_char); printp(); continue; } else p = prm; } else p = &buff[j]; /* find newlines */ add_headers = strdup(p); a_char = fnlines(add_headers); /* send data */ } else if (!iscmd("s", &buff[i], 1)) { /* No parameters */ if (j == i) { printf("send string: "); fgets(prm, 1024, stdin); prm[strlen(prm)-1] = '\0'; if (!strlen(prm)) { printp(); continue; } p = prm; } else p = &buff[j]; if (min && max) i_flag = min + (rand() % (max-min));#if defined _WIN32 /* sleep() is msec in Win32 */ sleep(i_flag*1000);#else sleep(i_flag);#endif /* forge URL */ /* No additional headers, no proxy: */ if ((a_char == "") && (p_char == "")) { sprintf(cmd, "GET %s%s HTTP/%s\r\n\r\n", url, p, v_char); /* No additional headers, using proxy: */ } else if ((a_char == "") && (p_char != "")) { if (h_flag == 80) sprintf(cmd, "GET http://%s%s%s HTTP/%s\r\n\r\n", h_char, url, p, v_char); else sprintf(cmd, "GET http://%s%s%s:%d HTTP/%s\r\n\r\n", h_char, url, p, h_flag, v_char); /* With additional headers, no proxy: */ } else if ((a_char != "") && (p_char == "")) { sprintf(cmd, "GET %s%s HTTP/%s\r\n" "%s\r\n\r\n", url, p, v_char, a_char); /* With additional headers, using proxy: */ } else { if (h_flag == 80) sprintf(cmd, "GET http://%s%s%s HTTP/%s\r\n" "%s\r\n\r\n", h_char, url, p, v_char, a_char); else sprintf(cmd, "GET http://%s%s%s:%d HTTP/%s\r\n" "%s\r\n\r\n", h_char, url, p, h_flag, v_char, a_char); } /* send message */ /* direct connection: */ if (p_char == "") send_msg(cmd, h_char, h_flag, r_flag, q_flag); /* use proxy server: */ else send_msg(cmd, p_char, p_flag, r_flag, q_flag); /* * No command found!! */ } else printf("unknown command '%s'\n", &buff[i]); /* print prompt after command execution */ printp(); } if (isatty(0)) printf("\n"); return (0);}/* Print prompt */voidprintp (void) { /* Only if stdin is connected to terminal */ if (isatty(0)) printf("wput > ");}/* Check for correct command syntax: */intiscmd(char *str, char *ptr, int n) { if (!strncmp(str, ptr, n) && (isspace(*(ptr + n)) || (*(ptr + n) == '\r') || (*(ptr + n) == '\n') || (*(ptr + n) == '\0'))) return (0); else return (1);}/* find newline chars ('\'+'n') and replace them with '\n' */char *fnlines(char *str) { char *tmpstr; char *retstr; /* Max. number of headers */ int n = 128; int i = 0; tmpstr = malloc(strlen(str) + n); retstr = tmpstr; while (*str != '\0' && i <= n) { if (*str == ';') { n++; *tmpstr++ = '\r'; *tmpstr++ = '\n'; } else *tmpstr++ = *str; str++; } *tmpstr = '\0'; return retstr;}#if !defined _WIN32voidcatchalarm(int signo) { printf("timed out\n"); if (isatty(0)) siglongjmp(env_alrm, 1); else exit (1);}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -