OSDN Git Service

WebGUIのレスポンスが悪かったのを修正
[peercast-im/PeerCastIM.git] / core / common / http.cpp
1 // ------------------------------------------------
2 // File : http.cpp
3 // Date: 4-apr-2002
4 // Author: giles
5 // Desc: 
6 //              HTTP protocol handling
7 //
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.
14
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 // ------------------------------------------------
20
21
22 #include <stdlib.h>
23 #include "http.h"
24 #include "sys.h"
25 #include "common.h"
26 #ifdef _DEBUG
27 #include "chkMemoryLeak.h"
28 #define DEBUG_NEW new(__FILE__, __LINE__)
29 #define new DEBUG_NEW
30 #endif
31
32 //-----------------------------------------
33 bool HTTP::checkResponse(int r)
34 {
35         if (readResponse()!=r)
36         {
37                 LOG_ERROR("Unexpected HTTP: %s",cmdLine);
38                 throw StreamException("Unexpected HTTP response");
39                 return false;
40         }
41         
42         return true;
43 }
44 //-----------------------------------------
45 void HTTP::readRequest()
46 {
47         readLine(cmdLine,sizeof(cmdLine));
48 }
49 //-----------------------------------------
50 int HTTP::readResponse()
51 {
52         readLine(cmdLine,sizeof(cmdLine));
53
54         char *cp = cmdLine;
55
56         while (*cp)     if (*++cp == ' ') break;
57         while (*cp) if (*++cp != ' ') break;
58
59         char *scp = cp;
60
61         while (*cp)     if (*++cp == ' ') break;
62         *cp = 0;
63
64         return atoi(scp);
65 }
66         
67 //-----------------------------------------
68 bool    HTTP::nextHeader()
69 {
70         if (readLine(cmdLine,sizeof(cmdLine)))
71         {
72                 char *ap = strstr(cmdLine,":");
73                 if (ap)
74                         while (*++ap)
75                                 if (*ap!=' ')
76                                         break;
77                 arg = ap;
78                 return true;
79         }else
80         {
81                 arg = NULL;
82                 return false;
83         }
84
85 }
86 //-----------------------------------------
87 bool    HTTP::isHeader(const char *hs)
88 {
89         return stristr(cmdLine,hs) != NULL;
90 }
91 //-----------------------------------------
92 bool    HTTP::isRequest(const char *rq)
93 {
94         return strncmp(cmdLine,rq,strlen(rq)) == 0;
95 }
96 //-----------------------------------------
97 char *HTTP::getArgStr()
98 {
99         return arg;
100 }
101 //-----------------------------------------
102 int     HTTP::getArgInt()
103 {
104         if (arg) 
105                 return atoi(arg);
106         else 
107                 return 0;
108 }
109 //-----------------------------------------
110 void HTTP::getAuthUserPass(char *user, char *pass, size_t szUser, size_t szPass)
111 {
112         if (arg)
113         {
114                 char *s = stristr(arg,"Basic");
115                 if (s)
116                 {
117                         while (*s)
118                                 if (*s++ == ' ')
119                                         break;
120                         String str;
121                         str.set(s,String::T_BASE64);
122                         str.convertTo(String::T_ASCII);
123                         s = strstr(str.cstr(),":");
124                         if (s)
125                         {
126                                 *s = 0;
127                                 if (user)
128                                 {
129                                         strncpy(user, str.cstr(), szUser);
130                                         user[szUser-1] = '\0';
131                                 }
132                                 if (pass)
133                                 {
134                                         strncpy(pass, s+1, szPass);
135                                         pass[szPass-1] = '\0';
136                                 }
137                         }                       
138                 }
139         }
140 }
141 // -----------------------------------
142 void    CookieList::init()
143 {
144         for(int i=0; i<MAX_COOKIES; i++)
145                 list[i].clear();
146
147         neverExpire = false;
148 }
149
150 // -----------------------------------
151 bool    CookieList::contains(Cookie &c)
152 {
153         if ((c.id[0]) && (c.ip))
154                 for(int i=0; i<MAX_COOKIES; i++)
155                         if (list[i].compare(c))
156                                 return true;
157
158         return false;
159 }
160 // -----------------------------------
161 void    Cookie::logDebug(const char *str, int ind)
162 {
163         char ipstr[64];
164         Host h;
165         h.ip = ip;
166         h.IPtoStr(ipstr);
167
168         LOG_DEBUG("%s %d: %s - %s",str,ind,ipstr,id);
169 }
170
171 // -----------------------------------
172 bool    CookieList::add(Cookie &c)
173 {
174         if (contains(c))
175                 return false;
176
177         unsigned int oldestTime=(unsigned int)-1; 
178         int oldestIndex=0;
179
180         for(int i=0; i<MAX_COOKIES; i++)
181                 if (list[i].time <= oldestTime)
182                 {
183                         oldestIndex = i;
184                         oldestTime = list[i].time;
185                 }
186
187         c.logDebug("Added cookie",oldestIndex);
188         c.time = sys->getTime();
189         list[oldestIndex]=c;
190         return true;
191 }
192 // -----------------------------------
193 void    CookieList::remove(Cookie &c)
194 {
195         for(int i=0; i<MAX_COOKIES; i++)
196                 if (list[i].compare(c))
197                         list[i].clear();
198 }