1 // ------------------------------------------------
6 // Servent handshaking, TODO: should be in its own class
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 // ------------------------------------------------
31 #include "chkMemoryLeak.h"
32 #define DEBUG_NEW new(__FILE__, __LINE__)
36 // -----------------------------------
37 static void termArgs(char *str)
41 int slen = strlen(str);
42 for(int i=0; i<slen; i++)
43 if (str[i]=='&') str[i] = 0;
46 // -----------------------------------
47 char *nextCGIarg(char *cp, char *cmd, char *arg)
64 if (cnt >= (MAX_CGI_LEN-1))
80 if (cnt >= (MAX_CGI_LEN-1))
87 // -----------------------------------
88 bool getCGIargBOOL(char *a)
90 return (strcmp(a,"1")==0);
92 // -----------------------------------
93 int getCGIargINT(char *a)
98 // -----------------------------------
99 void Servent::handshakeHTTP(HTTP &http, bool isHTTP)
101 char *in = http.cmdLine;
103 if (http.isRequest("GET /"))
107 char *pt = strstr(fn,HTTP_PROTO1);
111 if (strncmp(fn,"/admin?",7)==0)
113 if (!isAllowed(ALLOW_HTML))
114 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
117 LOG_DEBUG("Admin client");
120 }else if (strncmp(fn,"/http/",6)==0)
122 String dirName = fn+6;
124 if (!isAllowed(ALLOW_HTML))
125 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
127 if (!handshakeAuth(http,fn,false))
128 throw HTTPException(HTTP_SC_UNAUTHORIZED,401);
131 handshakeRemoteFile(dirName);
134 }else if (strncmp(fn,"/html/",6)==0)
136 String dirName = fn+1;
138 if (!isAllowed(ALLOW_HTML))
139 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
141 if (handshakeAuth(http,fn,true))
142 handshakeLocalFile(dirName);
145 }else if (strncmp(fn,"/admin/?",8)==0)
147 if (!isAllowed(ALLOW_HTML))
148 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
150 LOG_DEBUG("Admin client");
153 }else if (strncmp(fn,"/admin.cgi",10)==0)
155 if (!isAllowed(ALLOW_BROADCAST))
156 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
158 char *pwdArg = getCGIarg(fn,"pass=");
159 char *songArg = getCGIarg(fn,"song=");
160 char *mountArg = getCGIarg(fn,"mount=");
161 char *urlArg = getCGIarg(fn,"url=");
163 if (pwdArg && songArg)
166 int slen = strlen(fn);
167 for(i=0; i<slen; i++)
168 if (fn[i]=='&') fn[i] = 0;
170 Channel *c=chanMgr->channel;
173 if ((c->status == Channel::S_BROADCASTING) &&
174 (c->info.contentType == ChanInfo::T_MP3) )
176 // if we have a mount point then check for it, otherwise update all channels.
180 match = strcmp(c->mount,mountArg)==0;
184 ChanInfo newInfo = c->info;
185 newInfo.track.title.set(songArg,String::T_ESC);
186 newInfo.track.title.convertTo(String::T_UNICODE);
190 newInfo.track.contact.set(urlArg,String::T_ESC);
191 LOG_CHANNEL("Channel Shoutcast update: %s",songArg);
192 c->updateInfo(newInfo);
199 }else if (strncmp(fn,"/pls/",5)==0)
202 if (!sock->host.isLocalhost())
203 if (!isAllowed(ALLOW_DIRECT) || !isFiltered(ServFilter::F_DIRECT))
204 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
208 if (servMgr->getChannel(fn+5,info,isPrivate()))
209 handshakePLS(info,false);
211 throw HTTPException(HTTP_SC_NOTFOUND,404);
213 }else if (strncmp(fn,"/stream/",8)==0)
216 if (!sock->host.isLocalhost())
217 if (!isAllowed(ALLOW_DIRECT) || !isFiltered(ServFilter::F_DIRECT))
218 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
220 triggerChannel(fn+8,ChanInfo::SP_HTTP,isPrivate());
223 }else if (strncmp(fn,"/channel/",9)==0)
226 if (!sock->host.isLocalhost())
227 if (!isAllowed(ALLOW_NETWORK) || !isFiltered(ServFilter::F_NETWORK))
228 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
230 triggerChannel(fn+9,ChanInfo::SP_PCP,false);
234 while (http.nextHeader());
235 http.writeLine(HTTP_SC_FOUND);
236 http.writeLineF("Location: /%s/index.html",servMgr->htmlPath);
240 else if (http.isRequest("POST /"))
244 char *pt = strstr(fn,HTTP_PROTO1);
248 if (strncmp(fn,"/pls/",5)==0)
251 if (!sock->host.isLocalhost())
252 if (!isAllowed(ALLOW_DIRECT) || !isFiltered(ServFilter::F_DIRECT))
253 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
257 if (servMgr->getChannel(fn+5,info,isPrivate()))
258 handshakePLS(info,false);
260 throw HTTPException(HTTP_SC_NOTFOUND,404);
262 }else if (strncmp(fn,"/stream/",8)==0)
265 if (!sock->host.isLocalhost())
266 if (!isAllowed(ALLOW_DIRECT) || !isFiltered(ServFilter::F_DIRECT))
267 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
269 triggerChannel(fn+8,ChanInfo::SP_HTTP,isPrivate());
271 }else if (strncmp(fn,"/admin",7)==0)
273 if (!isAllowed(ALLOW_HTML))
274 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
277 LOG_DEBUG("Admin client");
278 while(http.nextHeader()){
279 LOG_DEBUG("%s",http.cmdLine);
282 if (sock->readLine(buf, sizeof(buf)) != 0){
288 while (http.nextHeader());
289 http.writeLine(HTTP_SC_FOUND);
290 http.writeLineF("Location: /%s/index.html",servMgr->htmlPath);
293 }else if (http.isRequest("GIV"))
297 while (http.nextHeader()) ;
299 if (!isAllowed(ALLOW_NETWORK))
300 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
305 char *idstr = strstr(in,"/");
310 sock->host.toStr(ipstr);
314 // at the moment we don`t really care where the GIV came from, so just give to chan. no. if its waiting.
315 Channel *ch = chanMgr->findChannelByID(id);
318 throw HTTPException(HTTP_SC_NOTFOUND,404);
320 if (!ch->acceptGIV(sock))
321 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
324 LOG_DEBUG("Accepted GIV channel %s from: %s",idstr,ipstr);
326 sock=NULL; // release this servent but dont close socket.
330 if (!servMgr->acceptGIV(sock))
331 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
333 LOG_DEBUG("Accepted GIV PCP from: %s",ipstr);
334 sock=NULL; // release this servent but dont close socket.
337 }else if (http.isRequest(PCX_PCP_CONNECT))
340 if (!isAllowed(ALLOW_NETWORK) || !isFiltered(ServFilter::F_NETWORK))
341 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
343 processIncomingPCP(true);
345 }else if (http.isRequest("PEERCAST CONNECT"))
347 if (!isAllowed(ALLOW_NETWORK) || !isFiltered(ServFilter::F_NETWORK))
348 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
350 LOG_DEBUG("PEERCAST client");
353 }else if (http.isRequest("SOURCE"))
355 if (!isAllowed(ALLOW_BROADCAST))
356 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
361 if (ps=strstr(in,"ICE/1.0"))
365 LOG_DEBUG("ICE 1.0 client to %s",mount?mount:"unknown");
367 mount = in+strlen(in);
371 mount[-1] = 0; // password preceeds
374 loginPassword.set(in+7);
376 LOG_DEBUG("ICY client: %s %s",loginPassword.cstr(),mount?mount:"unknown");
380 loginMount.set(mount);
382 handshakeICY(Channel::SRC_ICECAST,isHTTP);
383 sock = NULL; // socket is taken over by channel, so don`t close it
385 }else if (http.isRequest(servMgr->password))
387 if (!isAllowed(ALLOW_BROADCAST))
388 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
390 loginPassword.set(servMgr->password); // pwd already checked
392 sock->writeLine("OK2");
393 sock->writeLine("icy-caps:11");
395 LOG_DEBUG("ShoutCast client");
397 handshakeICY(Channel::SRC_SHOUTCAST,isHTTP);
398 sock = NULL; // socket is taken over by channel, so don`t close it
402 throw HTTPException(HTTP_SC_BADREQUEST,400);
406 // -----------------------------------
407 bool Servent::canStream(Channel *ch)
412 if (servMgr->isDisabled)
417 if (!ch->isPlaying() || ch->isFull() || ((type == T_DIRECT) && servMgr->directFull()))
420 if (!isIndexTxt(ch) && (type == T_RELAY) && (servMgr->relaysFull()))
423 Channel *c = chanMgr->channel;
425 unsigned int needRate = 0;
426 unsigned int allRate = 0;
429 int nlr = c->localRelays();
430 allRate += c->info.bitrate * nlr;
431 if ((c != ch) && (nlr == 0)){
432 if(!isIndexTxt(c)) // for PCRaw (relay)
434 needRate+=c->info.bitrate;
439 unsigned int numRelay = servMgr->numStreams(Servent::T_RELAY,false);
440 int diff = servMgr->maxRelays - numRelay;
441 if (ch->localRelays()){
450 LOG_DEBUG("Relay check: Max=%d Now=%d Need=%d ch=%d",
451 servMgr->maxBitrateOut, allRate, needRate, ch->info.bitrate);
452 // if ( !ch->isPlaying()
454 // || (allRate + needRate + ch->info.bitrate > servMgr->maxBitrateOut)
455 // || ((type == T_RELAY) && servMgr->relaysFull() && force_off) // for PCRaw (relay) (force_off)
456 // || ((type == T_RELAY) && (((servMgr->numStreams(Servent::T_RELAY,false) + noRelay) >= servMgr->maxRelays)) && force_off) // for PCRaw (relay) (force_off)
457 // || ((type == T_DIRECT) && servMgr->directFull())
460 if (allRate + needRate + ch->info.bitrate > servMgr->maxBitrateOut)
462 LOG_DEBUG("Relay check: NG");
466 if (!isIndexTxt(ch) && (type == T_RELAY) && (numRelay + noRelay >= servMgr->maxRelays))
468 LOG_DEBUG("Relay check: NG");
473 LOG_DEBUG("Relay check: OK");
476 // -----------------------------------
477 void Servent::handshakeIncoming()
480 setStatus(S_HANDSHAKE);
483 sock->readLine(buf,sizeof(buf));
486 sock->host.toStr(sb);
489 if (stristr(buf,RTSP_PROTO1))
491 LOG_DEBUG("RTSP from %s '%s'",sb,buf);
493 rtsp.initRequest(buf);
495 }else if (stristr(buf,HTTP_PROTO1))
497 LOG_DEBUG("HTTP from %s '%s'",sb,buf);
499 http.initRequest(buf);
500 handshakeHTTP(http,true);
503 LOG_DEBUG("Connect from %s '%s'",sb,buf);
505 http.initRequest(buf);
506 handshakeHTTP(http,false);
510 // -----------------------------------
511 void Servent::triggerChannel(char *str, ChanInfo::PROTOCOL proto,bool relay)
515 // WLockBlock lb(&(chanMgr->channellock));
517 // LOG_DEBUG("----------triggerChannel LOCK ON");
519 servMgr->getChannel(str,info,relay);
520 // LOG_DEBUG("==========triggerChannel LOCK OFF");
523 if (proto == ChanInfo::SP_PCP)
528 outputProtocol = proto;
530 processStream(false,info);
533 // -----------------------------------
534 void writePLSHeader(Stream &s, PlayList::TYPE type)
536 s.writeLine(HTTP_SC_OK);
537 s.writeLineF("%s %s",HTTP_HS_SERVER,PCX_AGENT);
542 case PlayList::T_PLS:
545 case PlayList::T_ASX:
548 case PlayList::T_RAM:
555 s.writeLineF("%s %s",HTTP_HS_CONTENT,content);
556 s.writeLine("Content-Disposition: inline");
557 s.writeLine("Cache-Control: private" );
558 s.writeLineF("%s %s",HTTP_HS_CONNECTION,"close");
563 // -----------------------------------
564 void Servent::handshakePLS(ChanInfo &info, bool doneHandshake)
571 while (sock->readLine(in,128));
574 if (getLocalTypeURL(url,info.contentType))
579 if ((info.contentType == ChanInfo::T_WMA) || (info.contentType == ChanInfo::T_WMV))
580 type = PlayList::T_ASX;
581 else if (info.contentType == ChanInfo::T_OGM)
582 type = PlayList::T_RAM;
584 type = PlayList::T_PLS;
586 writePLSHeader(*sock,type);
590 pls = new PlayList(type,1);
592 pls->addChannel(url,info);
599 // -----------------------------------
600 void Servent::handshakePLS(ChanHitList **cl, int num, bool doneHandshake)
606 while (sock->readLine(in,128));
608 if (getLocalURL(url))
610 writePLSHeader(*sock,PlayList::T_SCPLS);
614 pls = new PlayList(PlayList::T_SCPLS,num);
616 for(int i=0; i<num; i++)
617 pls->addChannel(url,cl[i]->info);
624 // -----------------------------------
625 bool Servent::getLocalURL(char *str)
628 throw StreamException("Not connected");
635 if (sock->host.localIP())
636 h = sock->getLocalHost();
638 h = servMgr->serverHost;
640 h.port = servMgr->serverHost.port;
644 sprintf(str,"http://%s",ipStr);
648 // -----------------------------------
649 bool Servent::getLocalTypeURL(char *str, ChanInfo::TYPE type)
652 throw StreamException("Not connected");
659 if (sock->host.localIP())
660 h = sock->getLocalHost();
662 h = servMgr->serverHost;
664 h.port = servMgr->serverHost.port;
668 case ChanInfo::T_WMA:
669 case ChanInfo::T_WMV:
670 sprintf(str,"mms://%s",ipStr);
673 sprintf(str,"http://%s",ipStr);
677 // -----------------------------------
678 // Warning: testing RTSP/RTP stuff below.
679 // .. moved over to seperate app now.
680 // -----------------------------------
681 void Servent::handshakePOST()
684 while (sock->readLine(tmp,sizeof(tmp)))
685 LOG_DEBUG("POST: %s",tmp);
687 throw HTTPException(HTTP_SC_BADREQUEST,400);
691 // -----------------------------------
692 void Servent::handshakeRTSP(RTSP &rtsp)
694 throw HTTPException(HTTP_SC_BADREQUEST,400);
696 // -----------------------------------
697 bool Servent::handshakeAuth(HTTP &http,const char *args,bool local)
699 char user[1024],pass[1024];
700 user[0] = pass[0] = 0;
702 char *pwd = getCGIarg(args, "pass=");
704 if ((pwd) && strlen(servMgr->password))
707 char *as = strstr(tmp.cstr(),"&");
709 if (strcmp(tmp,servMgr->password)==0)
711 while (http.nextHeader());
719 while (http.nextHeader())
721 char *arg = http.getArgStr();
725 switch (servMgr->authType)
727 case ServMgr::AUTH_HTTPBASIC:
728 if (http.isHeader("Authorization"))
729 http.getAuthUserPass(user,pass);
731 case ServMgr::AUTH_COOKIE:
732 if (http.isHeader("Cookie"))
734 LOG_DEBUG("Got cookie: %s",arg);
736 while ((idp = strstr(idp,"id=")))
739 gotCookie.set(idp,sock->host.ip);
740 if (servMgr->cookieList.contains(gotCookie))
742 LOG_DEBUG("Cookie found");
753 if (sock->host.isLocalhost())
757 switch (servMgr->authType)
759 case ServMgr::AUTH_HTTPBASIC:
761 if ((strcmp(pass,servMgr->password)==0) && strlen(servMgr->password))
764 case ServMgr::AUTH_COOKIE:
765 if (servMgr->cookieList.contains(cookie))
772 if (servMgr->authType == ServMgr::AUTH_HTTPBASIC)
774 http.writeLine(HTTP_SC_UNAUTHORIZED);
775 http.writeLine("WWW-Authenticate: Basic realm=\"PeerCast Admin\"");
776 }else if (servMgr->authType == ServMgr::AUTH_COOKIE)
778 String file = servMgr->htmlPath;
779 file.append("/login.html");
781 handshakeLocalFile(file);
783 handshakeRemoteFile(file);
790 // -----------------------------------
791 void Servent::handshakeCMD(char *cmd)
793 char result[MAX_CGI_LEN];
794 char arg[MAX_CGI_LEN];
795 char curr[MAX_CGI_LEN];
806 if (!handshakeAuth(http,cmd,true))
811 if (cmpCGIarg(cmd,"cmd=","redirect"))
813 char *j = getCGIarg(cmd,"url=");
818 url.set(j,String::T_ESC);
819 url.convertTo(String::T_ASCII);
821 if (!url.contains("http://"))
822 url.prepend("http://");
824 html.setRefreshURL(url.cstr());
828 html.startTagEnd("h3","Please wait...");
835 if (cmpCGIarg(cmd,"cmd=","viewxml"))
840 }else if (cmpCGIarg(cmd,"cmd=","clearlog"))
842 sys->logBuf->clear();
843 sprintf(jumpStr,"/%s/viewlog.html",servMgr->htmlPath);
846 }else if (cmpCGIarg(cmd,"cmd=","save"))
849 peercastInst->saveSettings();
851 sprintf(jumpStr,"/%s/settings.html",servMgr->htmlPath);
853 }else if (cmpCGIarg(cmd,"cmd=","reg"))
856 chanMgr->broadcastID.toStr(idstr);
857 sprintf(jumpStr,"http://www.peercast.org/register/?id=%s",idstr);
859 }else if (cmpCGIarg(cmd,"cmd=","edit_bcid"))
864 while (cp=nextCGIarg(cp,curr,arg))
866 if (strcmp(curr,"id")==0)
868 else if (strcmp(curr,"del")==0)
869 servMgr->removeValidBCID(id);
870 else if (strcmp(curr,"valid")==0)
872 bcid = servMgr->findValidBCID(id);
874 bcid->valid = getCGIargBOOL(arg);
877 peercastInst->saveSettings();
878 sprintf(jumpStr,"/%s/bcid.html",servMgr->htmlPath);
881 }else if (cmpCGIarg(cmd,"cmd=","add_bcid"))
883 BCID *bcid = new BCID();
887 while (cp=nextCGIarg(cp,curr,arg))
889 if (strcmp(curr,"id")==0)
890 bcid->id.fromStr(arg);
891 else if (strcmp(curr,"name")==0)
893 else if (strcmp(curr,"email")==0)
894 bcid->email.set(arg);
895 else if (strcmp(curr,"url")==0)
897 else if (strcmp(curr,"valid")==0)
898 bcid->valid = getCGIargBOOL(arg);
899 else if (strcmp(curr,"result")==0)
904 LOG_DEBUG("Adding BCID : %s",bcid->name.cstr());
905 servMgr->addValidBCID(bcid);
906 peercastInst->saveSettings();
909 http.writeLine(HTTP_SC_OK);
911 http.writeString("OK");
914 sprintf(jumpStr,"/%s/bcid.html",servMgr->htmlPath);
919 }else if (cmpCGIarg(cmd,"cmd=","apply"))
921 //servMgr->numFilters = 0;
922 ServFilter *currFilter=servMgr->filters;
923 bool beginfilt = false;
930 int newPort=servMgr->serverHost.port;
931 int enableGetName = 0;
932 int allowConnectPCST = 0;
935 while (cp=nextCGIarg(cp,curr,arg))
938 if (strcmp(curr,"serveractive")==0)
939 servMgr->autoServe = getCGIargBOOL(arg);
940 else if (strcmp(curr,"port")==0)
941 newPort = getCGIargINT(arg);
942 else if (strcmp(curr,"icymeta")==0)
944 int iv = getCGIargINT(arg);
946 else if (iv > 16384) iv = 16384;
948 chanMgr->icyMetaInterval = iv;
950 }else if (strcmp(curr,"passnew")==0)
951 strcpy(servMgr->password,arg);
952 else if (strcmp(curr,"root")==0)
953 servMgr->isRoot = getCGIargBOOL(arg);
954 else if (strcmp(curr,"brroot")==0)
955 brRoot = getCGIargBOOL(arg);
956 else if (strcmp(curr,"getupd")==0)
957 getUpd = getCGIargBOOL(arg);
958 else if (strcmp(curr,"huint")==0)
959 chanMgr->setUpdateInterval(getCGIargINT(arg));
960 else if (strcmp(curr,"forceip")==0)
961 servMgr->forceIP = arg;
962 else if (strcmp(curr,"htmlPath")==0)
964 strcpy(servMgr->htmlPath,"html/");
965 strcat(servMgr->htmlPath,arg);
966 }else if (strcmp(curr,"djmsg")==0)
969 msg.set(arg,String::T_ESC);
970 msg.convertTo(String::T_UNICODE);
971 chanMgr->setBroadcastMsg(msg);
973 else if (strcmp(curr,"pcmsg")==0)
975 servMgr->rootMsg.set(arg,String::T_ESC);
976 servMgr->rootMsg.convertTo(String::T_UNICODE);
977 }else if (strcmp(curr,"minpgnu")==0)
978 servMgr->minGnuIncoming = atoi(arg);
979 else if (strcmp(curr,"maxpgnu")==0)
980 servMgr->maxGnuIncoming = atoi(arg);
985 else if (strcmp(curr,"maxcin")==0)
986 servMgr->maxControl = getCGIargINT(arg);
988 else if (strcmp(curr,"maxup")==0)
989 servMgr->maxBitrateOut = getCGIargINT(arg);
990 else if (strcmp(curr,"maxrelays")==0)
991 servMgr->setMaxRelays(getCGIargINT(arg));
992 else if (strcmp(curr,"maxdirect")==0)
993 servMgr->maxDirect = getCGIargINT(arg);
994 else if (strcmp(curr,"maxrelaypc")==0)
995 chanMgr->maxRelaysPerChannel = getCGIargINT(arg);
996 else if (strncmp(curr,"filt_",5)==0)
999 servMgr->numFilters = 0;
1004 if (strncmp(fs,"ip",2)==0) // ip must be first
1006 currFilter = &servMgr->filters[servMgr->numFilters];
1008 currFilter->host.fromStrIP(arg,DEFAULT_PORT);
1009 if ((currFilter->host.ip) && (servMgr->numFilters < (ServMgr::MAX_FILTERS-1)))
1011 servMgr->numFilters++;
1012 servMgr->filters[servMgr->numFilters].init(); // clear new entry
1015 }else if (strncmp(fs,"bn",2)==0)
1016 currFilter->flags |= ServFilter::F_BAN;
1017 else if (strncmp(fs,"pr",2)==0)
1018 currFilter->flags |= ServFilter::F_PRIVATE;
1019 else if (strncmp(fs,"nw",2)==0)
1020 currFilter->flags |= ServFilter::F_NETWORK;
1021 else if (strncmp(fs,"di",2)==0)
1022 currFilter->flags |= ServFilter::F_DIRECT;
1027 else if (strcmp(curr,"clientactive")==0)
1028 servMgr->autoConnect = getCGIargBOOL(arg);
1029 else if (strcmp(curr,"yp")==0)
1033 String str(arg,String::T_ESC);
1034 str.convertTo(String::T_ASCII);
1035 servMgr->rootHost = str;
1038 else if (strcmp(curr,"yp2")==0)
1042 String str(arg,String::T_ESC);
1043 str.convertTo(String::T_ASCII);
1044 servMgr->rootHost2 = str;
1047 else if (strcmp(curr,"deadhitage")==0)
1048 chanMgr->deadHitAge = getCGIargINT(arg);
1049 else if (strcmp(curr,"refresh")==0)
1050 servMgr->refreshHTML = getCGIargINT(arg);
1051 else if (strcmp(curr,"auth")==0)
1053 if (strcmp(arg,"cookie")==0)
1054 servMgr->authType = ServMgr::AUTH_COOKIE;
1055 else if (strcmp(arg,"http")==0)
1056 servMgr->authType = ServMgr::AUTH_HTTPBASIC;
1058 }else if (strcmp(curr,"expire")==0)
1060 if (strcmp(arg,"session")==0)
1061 servMgr->cookieList.neverExpire = false;
1062 else if (strcmp(arg,"never")==0)
1063 servMgr->cookieList.neverExpire = true;
1066 else if (strcmp(curr,"logDebug")==0)
1067 showLog |= atoi(arg)?(1<<LogBuffer::T_DEBUG):0;
1068 else if (strcmp(curr,"logErrors")==0)
1069 showLog |= atoi(arg)?(1<<LogBuffer::T_ERROR):0;
1070 else if (strcmp(curr,"logNetwork")==0)
1071 showLog |= atoi(arg)?(1<<LogBuffer::T_NETWORK):0;
1072 else if (strcmp(curr,"logChannel")==0)
1073 showLog |= atoi(arg)?(1<<LogBuffer::T_CHANNEL):0;
1075 else if (strcmp(curr,"allowHTML1")==0)
1076 allowServer1 |= atoi(arg)?(ALLOW_HTML):0;
1077 else if (strcmp(curr,"allowNetwork1")==0)
1078 allowServer1 |= atoi(arg)?(ALLOW_NETWORK):0;
1079 else if (strcmp(curr,"allowBroadcast1")==0)
1080 allowServer1 |= atoi(arg)?(ALLOW_BROADCAST):0;
1081 else if (strcmp(curr,"allowDirect1")==0)
1082 allowServer1 |= atoi(arg)?(ALLOW_DIRECT):0;
1084 else if (strcmp(curr,"allowHTML2")==0)
1085 allowServer2 |= atoi(arg)?(ALLOW_HTML):0;
1086 else if (strcmp(curr,"allowBroadcast2")==0)
1087 allowServer2 |= atoi(arg)?(ALLOW_BROADCAST):0;
1090 else if (strcmp(curr, "autoRelayKeep") ==0)
1091 servMgr->autoRelayKeep = getCGIargINT(arg);
1092 else if (strcmp(curr, "autoMaxRelaySetting") ==0)
1093 servMgr->autoMaxRelaySetting = getCGIargINT(arg);
1094 else if (strcmp(curr, "autoBumpSkipCount") ==0)
1095 servMgr->autoBumpSkipCount = getCGIargINT(arg);
1096 else if (strcmp(curr, "kickPushStartRelays") ==0)
1097 servMgr->kickPushStartRelays = getCGIargINT(arg);
1098 else if (strcmp(curr, "kickPushInterval") ==0)
1099 servMgr->kickPushInterval = getCGIargINT(arg);
1100 else if (strcmp(curr, "allowConnectPCST") ==0)
1101 allowConnectPCST = atoi(arg) ? 1 : 0;
1102 else if (strcmp(curr, "enableGetName") ==0)
1103 enableGetName = atoi(arg)? 1 : 0;
1104 else if (strcmp(curr, "autoPort0Kick") ==0)
1105 servMgr->autoPort0Kick = getCGIargBOOL(arg);
1106 else if (strcmp(curr, "allowOnlyVP") ==0)
1107 servMgr->allowOnlyVP = getCGIargBOOL(arg);
1108 else if (strcmp(curr, "kickKeepTime") ==0)
1109 servMgr->kickKeepTime = getCGIargINT(arg);
1111 else if (strcmp(curr, "maxRelaysIndexTxt") ==0) // for PCRaw (relay)
1112 servMgr->maxRelaysIndexTxt = getCGIargINT(arg);
1117 servMgr->showLog = showLog;
1118 servMgr->allowServer1 = allowServer1;
1119 servMgr->allowServer2 = allowServer2;
1120 servMgr->enableGetName = enableGetName;
1121 servMgr->allowConnectPCST = allowConnectPCST;
1122 if (!(servMgr->allowServer1 & ALLOW_HTML) && !(servMgr->allowServer2 & ALLOW_HTML))
1123 servMgr->allowServer1 |= ALLOW_HTML;
1125 if (servMgr->serverHost.port != newPort)
1127 Host lh(ClientSocket::getIP(NULL),newPort);
1130 sprintf(jumpStr,"http://%s/%s/settings.html",ipstr,servMgr->htmlPath);
1132 servMgr->serverHost.port = newPort;
1133 servMgr->restartServer=true;
1134 //html.setRefresh(3);
1135 //html.setRefreshURL(jumpStr);
1138 // html.startBody();
1139 // html.startTagEnd("h1","Please wait...");
1146 //servMgr->serverHost.toStr(ipstr);
1147 //sprintf(jumpStr,"/%s/settings.html",ipstr,servMgr->htmlPath);
1152 sprintf(jumpStr,"/%s/settings.html",servMgr->htmlPath);
1156 peercastInst->saveSettings();
1158 peercastApp->updateSettings();
1160 if ((servMgr->isRoot) && (brRoot))
1161 servMgr->broadcastRootSettings(getUpd);
1167 }else if (cmpCGIarg(cmd,"cmd=","fetch"))
1174 while (cp=nextCGIarg(cp,curr,arg))
1176 if (strcmp(curr,"url")==0)
1178 curl.set(arg,String::T_ESC);
1179 curl.convertTo(String::T_UNICODE);
1180 }else if (strcmp(curr,"name")==0)
1182 info.name.set(arg,String::T_ESC);
1183 info.name.convertTo(String::T_UNICODE);
1184 }else if (strcmp(curr,"desc")==0)
1186 info.desc.set(arg,String::T_ESC);
1187 info.desc.convertTo(String::T_UNICODE);
1188 }else if (strcmp(curr,"genre")==0)
1190 info.genre.set(arg,String::T_ESC);
1191 info.genre.convertTo(String::T_UNICODE);
1192 }else if (strcmp(curr,"contact")==0)
1194 info.url.set(arg,String::T_ESC);
1195 info.url.convertTo(String::T_UNICODE);
1196 }else if (strcmp(curr,"bitrate")==0)
1198 info.bitrate = atoi(arg);
1199 }else if (strcmp(curr,"type")==0)
1201 info.contentType = ChanInfo::getTypeFromStr(arg);
1206 info.bcID = chanMgr->broadcastID;
1208 Channel *c = chanMgr->createChannel(info,NULL);
1210 c->startURL(curl.cstr());
1213 sprintf(jumpStr,"/%s/relays.html",servMgr->htmlPath);
1216 }else if (cmpCGIarg(cmd,"cmd=","stopserv"))
1220 while (cp=nextCGIarg(cp,curr,arg))
1222 if (strcmp(curr,"index")==0)
1224 Servent *s = servMgr->findServentByIndex(atoi(arg));
1229 sprintf(jumpStr,"/%s/connections.html",servMgr->htmlPath);
1233 }else if (cmpCGIarg(cmd,"cmd=","hitlist"))
1236 bool stayConnected=hasCGIarg(cmd,"relay");
1239 ChanHitList *chl = chanMgr->hitlist;
1245 sprintf(tmp,"c%d=",index);
1246 if (cmpCGIarg(cmd,tmp,"1"))
1249 if (!(c=chanMgr->findChannelByID(chl->info.id)))
1251 c = chanMgr->createChannel(chl->info,NULL);
1253 throw StreamException("out of channels");
1254 c->stayConnected = stayConnected;
1263 char *findArg = getCGIarg(cmd,"keywords=");
1265 if (hasCGIarg(cmd,"relay"))
1268 sprintf(jumpStr,"/%s/relays.html",servMgr->htmlPath);
1271 }else if (cmpCGIarg(cmd,"cmd=","clear"))
1274 while (cp=nextCGIarg(cp,curr,arg))
1276 if (strcmp(curr,"hostcache")==0)
1277 servMgr->clearHostCache(ServHost::T_SERVENT);
1278 else if (strcmp(curr,"hitlists")==0)
1279 chanMgr->clearHitLists();
1280 else if (strcmp(curr,"packets")==0)
1282 stats.clearRange(Stats::PACKETSSTART,Stats::PACKETSEND);
1283 servMgr->numVersions = 0;
1287 sprintf(jumpStr,"/%s/index.html",servMgr->htmlPath);
1290 }else if (cmpCGIarg(cmd,"cmd=","upgrade"))
1292 if (servMgr->downloadURL[0])
1294 sprintf(jumpStr,"/admin?cmd=redirect&url=%s",servMgr->downloadURL);
1301 }else if (cmpCGIarg(cmd,"cmd=","connect"))
1305 Servent *s = servMgr->servents;
1308 sprintf(tmp,"c%d=",s->serventIndex);
1309 if (cmpCGIarg(cmd,tmp,"1"))
1311 if (hasCGIarg(cmd,"stop"))
1312 s->thread.active = false;
1316 sprintf(jumpStr,"/%s/connections.html",servMgr->htmlPath);
1319 }else if (cmpCGIarg(cmd,"cmd=","shutdown"))
1321 servMgr->shutdownTimer = 1;
1323 }else if (cmpCGIarg(cmd,"cmd=","stop"))
1327 while (cp=nextCGIarg(cp,curr,arg))
1329 if (strcmp(curr,"id")==0)
1333 Channel *c = chanMgr->findChannelByID(id);
1335 c->thread.active = false;
1336 c->thread.finish = true;
1340 sprintf(jumpStr,"/%s/relays.html",servMgr->htmlPath);
1343 }else if (cmpCGIarg(cmd,"cmd=","bump"))
1347 while (cp=nextCGIarg(cp,curr,arg))
1349 if (strcmp(curr,"id")==0)
1353 Channel *c = chanMgr->findChannelByID(id);
1357 sprintf(jumpStr,"/%s/relays.html",servMgr->htmlPath);
1360 }else if (cmpCGIarg(cmd,"cmd=","keep"))
1364 while (cp=nextCGIarg(cp,curr,arg))
1366 if (strcmp(curr,"id")==0)
1370 Channel *c = chanMgr->findChannelByID(id);
1373 //c->stayConnected = true;
1374 if (!c->stayConnected)
1376 //if (servMgr->getFirewall() == ServMgr::FW_OFF)
1377 c->stayConnected = true;
1380 c->stayConnected = false;
1383 sprintf(jumpStr,"/%s/relays.html",servMgr->htmlPath);
1386 }else if (cmpCGIarg(cmd,"cmd=","relay"))
1390 while (cp=nextCGIarg(cp,curr,arg))
1392 if (strcmp(curr,"id")==0)
1393 info.id.fromStr(arg);
1397 if (!chanMgr->findChannelByID(info.id))
1400 ChanHitList *chl = chanMgr->findHitList(info);
1402 throw StreamException("channel not found");
1405 Channel *c = chanMgr->createChannel(chl->info,NULL);
1407 throw StreamException("out of channels");
1409 c->stayConnected = true;
1413 sprintf(jumpStr,"/%s/relays.html",servMgr->htmlPath);
1417 }else if (cmpCGIarg(cmd,"net=","add"))
1422 while (cmd=nextCGIarg(cmd,curr,arg))
1424 if (strcmp(curr,"ip")==0)
1427 h.fromStrIP(arg,DEFAULT_PORT);
1428 if (servMgr->addOutgoing(h,id,true))
1429 LOG_NETWORK("Added connection: %s",arg);
1431 }else if (strcmp(curr,"id")==0)
1438 }else if (cmpCGIarg(cmd,"cmd=","logout"))
1441 servMgr->cookieList.remove(cookie);
1443 }else if (cmpCGIarg(cmd,"cmd=","login"))
1450 cookie.set(idstr,sock->host.ip);
1451 servMgr->cookieList.add(cookie);
1453 http.writeLine(HTTP_SC_FOUND);
1454 if (servMgr->cookieList.neverExpire)
1455 http.writeLineF("%s id=%s; path=/; expires=\"Mon, 01-Jan-3000 00:00:00 GMT\";",HTTP_HS_SETCOOKIE,idstr);
1457 http.writeLineF("%s id=%s; path=/;",HTTP_HS_SETCOOKIE,idstr);
1458 http.writeLineF("Location: /%s/index.html",servMgr->htmlPath);
1461 }else if (cmpCGIarg(cmd,"cmd=","setmeta"))
1464 while (cp=nextCGIarg(cp,curr,arg))
1466 if (strcmp(curr,"name")==0)
1469 chname.set(arg,String::T_ESC);
1470 chname.convertTo(String::T_ASCII);
1471 for(int i=0; i<ChanMgr::MAX_CHANNELS; i++)
1473 Channel *c = &chanMgr->channels[i];
1474 if ((c->isActive()) && (c->status == Channel::S_BROADCASTING) && (strcmp(c->info.name.cstr(),chname.cstr())==0))
1476 ChanInfo newInfo = c->info;
1478 while (cmd=nextCGIarg(cmd,curr,arg))
1481 chmeta.set(arg,String::T_ESC);
1482 chmeta.convertTo(String::T_ASCII);
1483 if (strcmp(curr,"desc")==0)
1484 newInfo.desc = chmeta.cstr();
1485 else if (strcmp(curr,"url")==0)
1486 newInfo.url = chmeta.cstr();
1487 else if (strcmp(curr,"genre")==0)
1488 newInfo.genre = chmeta.cstr();
1489 else if (strcmp(curr,"comment")==0)
1490 newInfo.comment = chmeta.cstr();
1491 else if (strcmp(curr,"t_contact")==0)
1492 newInfo.track.contact = chmeta.cstr();
1493 else if (strcmp(curr,"t_title")==0)
1494 newInfo.track.title = chmeta.cstr();
1495 else if (strcmp(curr,"t_artist")==0)
1496 newInfo.track.artist = chmeta.cstr();
1497 else if (strcmp(curr,"t_album")==0)
1498 newInfo.track.album = chmeta.cstr();
1499 else if (strcmp(curr,"t_genre")==0)
1500 newInfo.track.genre = chmeta.cstr();
1502 c->updateInfo(newInfo);
1504 newInfo.id.toStr(idstr);
1505 sprintf(jumpStr,"/%s/relayinfo.html?id=%s",servMgr->htmlPath,idstr);
1511 chname.set(arg,String::T_ESC);
1512 chname.convertTo(String::T_ASCII);
1514 Channel *c = chanMgr->findChannelByName(chname.cstr());
1515 if (c && (c->isActive()) && (c->status == Channel::S_BROADCASTING)){
1516 ChanInfo newInfo = c->info;
1517 while (cmd=nextCGIarg(cmd,curr,arg))
1520 chmeta.set(arg,String::T_ESC);
1521 chmeta.convertTo(String::T_ASCII);
1522 if (strcmp(curr,"desc")==0)
1523 newInfo.desc = chmeta.cstr();
1524 else if (strcmp(curr,"url")==0)
1525 newInfo.url = chmeta.cstr();
1526 else if (strcmp(curr,"genre")==0)
1527 newInfo.genre = chmeta.cstr();
1528 else if (strcmp(curr,"comment")==0)
1529 newInfo.comment = chmeta.cstr();
1530 else if (strcmp(curr,"t_contact")==0)
1531 newInfo.track.contact = chmeta.cstr();
1532 else if (strcmp(curr,"t_title")==0)
1533 newInfo.track.title = chmeta.cstr();
1534 else if (strcmp(curr,"t_artist")==0)
1535 newInfo.track.artist = chmeta.cstr();
1536 else if (strcmp(curr,"t_album")==0)
1537 newInfo.track.album = chmeta.cstr();
1538 else if (strcmp(curr,"t_genre")==0)
1539 newInfo.track.genre = chmeta.cstr();
1541 c->updateInfo(newInfo);
1543 newInfo.id.toStr(idstr);
1544 sprintf(jumpStr,"/%s/relayinfo.html?id=%s",servMgr->htmlPath,idstr);
1556 sprintf(jumpStr,"/%s/index.html",servMgr->htmlPath);
1561 }catch(StreamException &e)
1563 html.startTagEnd("h1","ERROR - %s",e.msg);
1564 LOG_ERROR("html: %s",e.msg);
1572 String jmp(jumpArg,String::T_HTML);
1573 jmp.convertTo(String::T_ASCII);
1574 html.locateTo(jmp.cstr());
1580 // -----------------------------------
1581 static XML::Node *createChannelXML(Channel *c)
1583 XML::Node *n = c->info.createChannelXML();
1584 n->add(c->createRelayXML(true));
1585 n->add(c->info.createTrackXML());
1586 // n->add(c->info.createServentXML());
1589 // -----------------------------------
1590 static XML::Node *createChannelXML(ChanHitList *chl)
1592 XML::Node *n = chl->info.createChannelXML();
1593 n->add(chl->createXML());
1594 n->add(chl->info.createTrackXML());
1595 // n->add(chl->info.createServentXML());
1598 // -----------------------------------
1599 void Servent::handshakeXML()
1607 XML::Node *rn = new XML::Node("peercast");
1611 rn->add(new XML::Node("servent uptime=\"%d\"",servMgr->getUptime()));
1613 rn->add(new XML::Node("bandwidth out=\"%d\" in=\"%d\"",
1614 stats.getPerSecond(Stats::BYTESOUT)-stats.getPerSecond(Stats::LOCALBYTESOUT),
1615 stats.getPerSecond(Stats::BYTESIN)-stats.getPerSecond(Stats::LOCALBYTESIN)
1618 rn->add(new XML::Node("connections total=\"%d\" relays=\"%d\" direct=\"%d\"",servMgr->totalConnected(),servMgr->numStreams(Servent::T_RELAY,true),servMgr->numStreams(Servent::T_DIRECT,true)));
1620 XML::Node *an = new XML::Node("channels_relayed total=\"%d\"",chanMgr->numChannels());
1623 Channel *c = chanMgr->channel;
1627 an->add(createChannelXML(c));
1632 // add public channels
1634 XML::Node *fn = new XML::Node("channels_found total=\"%d\"",chanMgr->numHitLists());
1637 ChanHitList *chl = chanMgr->hitlist;
1641 fn->add(createChannelXML(chl));
1648 if (servMgr->isRoot)
1650 // add private channels
1652 XML::Node *pn = new XML::Node("priv_channels");
1655 ChanHitList *chl = chanMgr->hitlist;
1659 if (chl->info.isPrivate())
1660 pn->add(createChannelXML(chl));
1667 XML::Node *hc = new XML::Node("host_cache");
1668 for(i=0; i<ServMgr::MAX_HOSTCACHE; i++)
1670 ServHost *sh = &servMgr->hostCache[i];
1671 if (sh->type != ServHost::T_NONE)
1674 sh->host.toStr(ipstr);
1676 hc->add(new XML::Node("host ip=\"%s\" type=\"%s\" time=\"%d\"",ipstr,ServHost::getTypeStr(sh->type),sh->time));
1683 sock->writeLine(HTTP_SC_OK);
1684 sock->writeLineF("%s %s",HTTP_HS_SERVER,PCX_AGENT);
1685 sock->writeLineF("%s %s",HTTP_HS_CONTENT,MIME_XML);
1686 sock->writeLine("Connection: close");
1688 sock->writeLine("");
1693 // -----------------------------------
1694 void Servent::readICYHeader(HTTP &http, ChanInfo &info, char *pwd)
1696 char *arg = http.getArgStr();
1699 if (http.isHeader("x-audiocast-name") || http.isHeader("icy-name") || http.isHeader("ice-name"))
1701 info.name.set(arg,String::T_ASCII);
1702 info.name.convertTo(String::T_UNICODE);
1704 }else if (http.isHeader("x-audiocast-url") || http.isHeader("icy-url") || http.isHeader("ice-url"))
1705 info.url.set(arg,String::T_ASCII);
1706 else if (http.isHeader("x-audiocast-bitrate") || (http.isHeader("icy-br")) || http.isHeader("ice-bitrate") || http.isHeader("icy-bitrate"))
1707 info.bitrate = atoi(arg);
1708 else if (http.isHeader("x-audiocast-genre") || http.isHeader("ice-genre") || http.isHeader("icy-genre"))
1710 info.genre.set(arg,String::T_ASCII);
1711 info.genre.convertTo(String::T_UNICODE);
1713 }else if (http.isHeader("x-audiocast-description") || http.isHeader("ice-description"))
1715 info.desc.set(arg,String::T_ASCII);
1716 info.desc.convertTo(String::T_UNICODE);
1718 }else if (http.isHeader("Authorization"))
1719 http.getAuthUserPass(NULL,pwd);
1720 else if (http.isHeader(PCX_HS_CHANNELID))
1721 info.id.fromStr(arg);
1722 else if (http.isHeader("ice-password"))
1725 if (strlen(arg) < 64)
1727 }else if (http.isHeader("content-type"))
1729 if (stristr(arg,MIME_OGG))
1730 info.contentType = ChanInfo::T_OGG;
1731 else if (stristr(arg,MIME_XOGG))
1732 info.contentType = ChanInfo::T_OGG;
1734 else if (stristr(arg,MIME_MP3))
1735 info.contentType = ChanInfo::T_MP3;
1736 else if (stristr(arg,MIME_XMP3))
1737 info.contentType = ChanInfo::T_MP3;
1739 else if (stristr(arg,MIME_WMA))
1740 info.contentType = ChanInfo::T_WMA;
1741 else if (stristr(arg,MIME_WMV))
1742 info.contentType = ChanInfo::T_WMV;
1743 else if (stristr(arg,MIME_ASX))
1744 info.contentType = ChanInfo::T_ASX;
1746 else if (stristr(arg,MIME_NSV))
1747 info.contentType = ChanInfo::T_NSV;
1748 else if (stristr(arg,MIME_RAW))
1749 info.contentType = ChanInfo::T_RAW;
1751 else if (stristr(arg,MIME_MMS))
1752 info.srcProtocol = ChanInfo::SP_MMS;
1753 else if (stristr(arg,MIME_XPCP))
1754 info.srcProtocol = ChanInfo::SP_PCP;
1755 else if (stristr(arg,MIME_XPEERCAST))
1756 info.srcProtocol = ChanInfo::SP_PEERCAST;
1758 else if (stristr(arg,MIME_XSCPLS))
1759 info.contentType = ChanInfo::T_PLS;
1760 else if (stristr(arg,MIME_PLS))
1761 info.contentType = ChanInfo::T_PLS;
1762 else if (stristr(arg,MIME_XPLS))
1763 info.contentType = ChanInfo::T_PLS;
1764 else if (stristr(arg,MIME_M3U))
1765 info.contentType = ChanInfo::T_PLS;
1766 else if (stristr(arg,MIME_MPEGURL))
1767 info.contentType = ChanInfo::T_PLS;
1768 else if (stristr(arg,MIME_TEXT))
1769 info.contentType = ChanInfo::T_PLS;
1776 // -----------------------------------
1777 void Servent::handshakeICY(Channel::SRC_TYPE type, bool isHTTP)
1783 // default to mp3 for shoutcast DSP (doesn`t send content-type)
1784 if (type == Channel::SRC_SHOUTCAST)
1785 info.contentType = ChanInfo::T_MP3;
1787 while (http.nextHeader())
1789 LOG_DEBUG("ICY %s",http.cmdLine);
1790 readICYHeader(http,info,loginPassword.cstr());
1795 // check password before anything else, if needed
1796 if (!loginPassword.isSame(servMgr->password))
1798 if (!sock->host.isLocalhost() || !loginPassword.isEmpty())
1799 throw HTTPException(HTTP_SC_UNAUTHORIZED,401);
1803 // we need a valid IP address before we start
1804 servMgr->checkFirewall();
1807 // attach channel ID to name, channel ID is also encoded with IP address
1808 // to help prevent channel hijacking.
1811 info.id = chanMgr->broadcastID;
1812 info.id.encode(NULL,info.name.cstr(),loginMount.cstr(),info.bitrate);
1814 LOG_DEBUG("Incoming source: %s : %s",info.name.cstr(),ChanInfo::getTypeStr(info.contentType));
1818 sock->writeStringF("%s\n\n",HTTP_SC_OK);
1820 sock->writeLine("OK");
1822 Channel *c = chanMgr->findChannelByID(info.id);
1825 LOG_CHANNEL("ICY channel already active, closing old one");
1826 c->thread.shutdown();
1830 info.comment = chanMgr->broadcastMsg;
1831 info.bcID = chanMgr->broadcastID;
1833 c = chanMgr->createChannel(info,loginMount.cstr());
1835 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
1837 c->startICY(sock,type);
1841 // -----------------------------------
1842 void Servent::handshakeLocalFile(const char *fn)
1847 if (servMgr->getModulePath) //JP-EX
1849 peercastApp->getDirectory();
1850 fileName = servMgr->modulePath;
1852 fileName = peercastApp->getPath();
1854 fileName.append(fn);
1856 LOG_DEBUG("Writing HTML file: %s",fileName.cstr());
1858 HTML html("",*sock);
1860 char *args = strstr(fileName.cstr(),"?");
1864 if (fileName.contains(".htm"))
1866 html.writeOK(MIME_HTML);
1867 html.writeTemplate(fileName.cstr(),args);
1869 }else if (fileName.contains(".css"))
1871 html.writeOK(MIME_CSS);
1872 html.writeRawFile(fileName.cstr());
1873 }else if (fileName.contains(".jpg"))
1875 html.writeOK(MIME_JPEG);
1876 html.writeRawFile(fileName.cstr());
1877 }else if (fileName.contains(".gif"))
1879 html.writeOK(MIME_GIF);
1880 html.writeRawFile(fileName.cstr());
1881 }else if (fileName.contains(".png"))
1883 html.writeOK(MIME_PNG);
1884 html.writeRawFile(fileName.cstr());
1888 // -----------------------------------
1889 void Servent::handshakeRemoteFile(const char *dirName)
1891 ClientSocket *rsock = sys->createSocket();
1893 throw HTTPException(HTTP_SC_UNAVAILABLE,503);
1896 const char *hostName = "www.peercast.org"; // hardwired for "security"
1899 host.fromStrName(hostName,80);
1907 rhttp.writeLineF("GET /%s HTTP/1.0",dirName);
1908 rhttp.writeLineF("%s %s",HTTP_HS_HOST,hostName);
1909 rhttp.writeLineF("%s %s",HTTP_HS_CONNECTION,"close");
1910 rhttp.writeLineF("%s %s",HTTP_HS_ACCEPT,"*/*");
1911 rhttp.writeLine("");
1914 bool isTemplate = false;
1915 while (rhttp.nextHeader())
1917 char *arg = rhttp.getArgStr();
1920 if (rhttp.isHeader("content-type"))
1925 MemoryStream mem(100*1024);
1926 while (!rsock->eof())
1930 len = rsock->readUpto(buf,sizeof(buf));
1939 int fileLen = mem.getPosition();
1944 if (contentType.contains(MIME_HTML))
1947 sock->writeLine(HTTP_SC_OK);
1948 sock->writeLineF("%s %s",HTTP_HS_SERVER,PCX_AGENT);
1949 sock->writeLineF("%s %s",HTTP_HS_CACHE,"no-cache");
1950 sock->writeLineF("%s %s",HTTP_HS_CONNECTION,"close");
1951 sock->writeLineF("%s %s",HTTP_HS_CONTENT,contentType.cstr());
1953 sock->writeLine("");
1957 HTML html("",*sock);
1958 html.readTemplate(mem,sock,0);
1960 sock->write(mem.buf,fileLen);