1 // ------------------------------------------------
6 // HTTP protocol handling
8 // (c) 2002 peercast.org
9 // ------------------------------------------------
10 // This program is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation; either version 2 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 // ------------------------------------------------
27 #include "chkMemoryLeak.h"
28 #define DEBUG_NEW new(__FILE__, __LINE__)
32 //-----------------------------------------
33 bool HTTP::checkResponse(int r)
35 if (readResponse()!=r)
37 LOG_ERROR("Unexpected HTTP: %s",cmdLine);
38 throw StreamException("Unexpected HTTP response");
44 //-----------------------------------------
45 void HTTP::readRequest()
47 readLine(cmdLine,sizeof(cmdLine));
49 //-----------------------------------------
50 int HTTP::readResponse()
52 readLine(cmdLine,sizeof(cmdLine));
56 while (*cp) if (*++cp == ' ') break;
57 while (*cp) if (*++cp != ' ') break;
61 while (*cp) if (*++cp == ' ') break;
67 //-----------------------------------------
68 bool HTTP::nextHeader()
70 if (readLine(cmdLine,sizeof(cmdLine)))
72 char *ap = strstr(cmdLine,":");
86 //-----------------------------------------
87 bool HTTP::isHeader(const char *hs)
89 return stristr(cmdLine,hs) != NULL;
91 //-----------------------------------------
92 bool HTTP::isRequest(const char *rq)
94 return strncmp(cmdLine,rq,strlen(rq)) == 0;
96 //-----------------------------------------
97 char *HTTP::getArgStr()
101 //-----------------------------------------
102 int HTTP::getArgInt()
109 //-----------------------------------------
110 void HTTP::getAuthUserPass(char *user, char *pass, size_t szUser, size_t szPass)
114 char *s = stristr(arg,"Basic");
121 str.set(s,String::T_BASE64);
122 str.convertTo(String::T_ASCII);
123 s = strstr(str.cstr(),":");
129 strncpy(user, str.cstr(), szUser);
130 user[szUser-1] = '\0';
134 strncpy(pass, s+1, szPass);
135 pass[szPass-1] = '\0';
141 // -----------------------------------
142 void CookieList::init()
144 for(int i=0; i<MAX_COOKIES; i++)
150 // -----------------------------------
151 bool CookieList::contains(Cookie &c)
153 if ((c.id[0]) && (c.ip))
154 for(int i=0; i<MAX_COOKIES; i++)
155 if (list[i].compare(c))
160 // -----------------------------------
161 void Cookie::logDebug(const char *str, int ind)
168 LOG_DEBUG("%s %d: %s - %s",str,ind,ipstr,id);
171 // -----------------------------------
172 bool CookieList::add(Cookie &c)
177 unsigned int oldestTime=(unsigned int)-1;
180 for(int i=0; i<MAX_COOKIES; i++)
181 if (list[i].time <= oldestTime)
184 oldestTime = list[i].time;
187 c.logDebug("Added cookie",oldestIndex);
188 c.time = sys->getTime();
192 // -----------------------------------
193 void CookieList::remove(Cookie &c)
195 for(int i=0; i<MAX_COOKIES; i++)
196 if (list[i].compare(c))