--------------------- PatchSet 7974 Date: 2006/05/25 19:24:25 Author: serassio Branch: nt Tag: (none) Log: Recovered lost Windows changes after merge with HEAD Members: tools/cachemgr.c:1.1.4.2->1.1.4.3 Index: squid/tools/cachemgr.c =================================================================== RCS file: /cvsroot/squid-sf//squid/tools/cachemgr.c,v retrieving revision 1.1.4.2 retrieving revision 1.1.4.3 diff -u -r1.1.4.2 -r1.1.4.3 --- squid/tools/cachemgr.c 24 May 2006 06:43:52 -0000 1.1.4.2 +++ squid/tools/cachemgr.c 25 May 2006 19:24:25 -0000 1.1.4.3 @@ -1,6 +1,6 @@ /* - * $Id: cachemgr.c,v 1.1.4.2 2006/05/24 06:43:52 serassio Exp $ + * $Id: cachemgr.c,v 1.1.4.3 2006/05/25 19:24:25 serassio Exp $ * * DEBUG: section 0 CGI Cache Manager * AUTHOR: Duane Wessels @@ -179,7 +179,11 @@ static void error_html(const char *msg); static char *menu_url(cachemgr_request * req, const char *action); static int parse_status_line(const char *sline, const char **statusStr); +#ifdef _SQUID_MSWIN_ +static cachemgr_request *read_request(char *); +#else static cachemgr_request *read_request(void); +#endif static char *read_get_request(void); static char *read_post_request(void); @@ -190,6 +194,45 @@ static int check_target_acl(const char *hostname, int port); +#ifdef _SQUID_MSWIN_ +static int s_iInitCount = 0; +int Win32SockInit(void) +{ + int iVersionRequested; + WSADATA wsaData; + int err; + + if (s_iInitCount > 0) { + s_iInitCount++; + return (0); + } + else if (s_iInitCount < 0) + return (s_iInitCount); + + /* s_iInitCount == 0. Do the initailization */ + iVersionRequested = MAKEWORD(2, 0); + err = WSAStartup((WORD) iVersionRequested, &wsaData); + if (err) { + s_iInitCount = -1; + return (s_iInitCount); + } + if (LOBYTE(wsaData.wVersion) != 2 || + HIBYTE(wsaData.wVersion) != 0) { + s_iInitCount = -2; + WSACleanup(); + return (s_iInitCount); + } + s_iInitCount++; + return (s_iInitCount); +} + +void Win32SockCleanup(void) +{ + if (--s_iInitCount == 0) + WSACleanup(); + return; +} +#endif /* ifdef _SQUID_MSWIN_ */ static const char * safe_str(const char *str) @@ -460,7 +503,12 @@ read_reply(int s, cachemgr_request * req) { char buf[4 * 1024]; +#ifdef _SQUID_MSWIN_ + int reply; + FILE *fp = tmpfile(); +#else FILE *fp = fdopen(s, "r"); +#endif /* interpretation states */ enum { isStatusLine, isHeaders, isBodyStart, isBody, isForward, isEof, isForwardEof, isSuccess, isError @@ -477,6 +525,11 @@ perror("fdopen"); return 1; } +#ifdef _SQUID_MSWIN_ + while ((reply = recv(s, buf, sizeof(buf), 0)) > 0) + fwrite(buf, 1, reply, fp); + rewind(fp); +#endif if (parse_menu) action = "menu"; /* read reply interpreting one line at a time depending on state */ @@ -572,6 +625,9 @@ static struct sockaddr_in S; int s; int l; +#ifdef _SQUID_MSWIN_ + int answer; +#endif static char buf[2 * 1024]; if (req == NULL) { auth_html(CACHEMGR_HOSTNAME, CACHE_HTTP_PORT, ""); @@ -626,9 +682,19 @@ req->hostname, req->action, make_auth_header(req)); +#ifdef _SQUID_MSWIN_ + send(s, buf, l, 0); +#else write(s, buf, l); +#endif debug(1) fprintf(stderr, "wrote request: '%s'\n", buf); +#ifdef _SQUID_MSWIN_ + answer=read_reply(s, req); + closesocket(s); + return answer; +#else return read_reply(s, req); +#endif } int @@ -636,16 +702,34 @@ { char *s; cachemgr_request *req; +#ifdef _SQUID_MSWIN_ + int answer; +#endif safe_inet_addr("255.255.255.255", &no_addr); now = time(NULL); +#ifdef _SQUID_MSWIN_ + Win32SockInit(); + _setmode( _fileno( stdin ), _O_BINARY ); + _setmode( _fileno( stdout ), _O_BINARY ); + _fmode = _O_BINARY; + if ((s = strrchr(argv[0], '\\'))) +#else if ((s = strrchr(argv[0], '/'))) +#endif progname = xstrdup(s + 1); else progname = xstrdup(argv[0]); if ((s = getenv("SCRIPT_NAME")) != NULL) script_name = xstrdup(s); +#ifdef _SQUID_MSWIN_ + req = read_request(NULL); + answer=process_request(req); + Win32SockCleanup(); + return answer; +#else req = read_request(); return process_request(req); +#endif } static char * read_post_request(void) @@ -674,10 +758,17 @@ return NULL; return xstrdup(s); } + +#ifdef _SQUID_MSWIN_ +static cachemgr_request * +read_request(char* buf) +{ +#else static cachemgr_request * read_request(void) { char *buf; +#endif cachemgr_request *req; char *s; char *t; @@ -688,7 +779,11 @@ (void) 0; else return NULL; +#ifdef _SQUID_MSWIN_ + if (strlen(buf) == 0 || strlen(buf) == 4000) +#else if (strlen(buf) == 0) +#endif return NULL; req = xcalloc(1, sizeof(cachemgr_request)); for (s = strtok(buf, "&"); s != NULL; s = strtok(NULL, "&")) {