• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeioslave/http
 

tdeioslave/http

  • tdeioslave
  • http
  • kcookiejar
kcookieserver.cpp
1 /*
2 This file is part of KDE
3 
4  Copyright (C) 1998-2000 Waldo Bastian (bastian@kde.org)
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 //----------------------------------------------------------------------------
24 //
25 // KDE Cookie Server
26 // $Id$
27 
28 #define SAVE_DELAY 3 // Save after 3 minutes
29 
30 #include <unistd.h>
31 
32 #include <tqtimer.h>
33 #include <tqptrlist.h>
34 #include <tqfile.h>
35 
36 #include <dcopclient.h>
37 
38 #include <tdeconfig.h>
39 #include <kdebug.h>
40 #include <tdeapplication.h>
41 #include <tdecmdlineargs.h>
42 #include <kstandarddirs.h>
43 
44 #include "kcookiejar.h"
45 #include "kcookiewin.h"
46 #include "kcookieserver.h"
47 
48 extern "C" {
49  KDE_EXPORT KDEDModule *create_kcookiejar(const TQCString &name)
50  {
51  return new KCookieServer(name);
52  }
53 }
54 
55 
56 // Cookie field indexes
57 enum CookieDetails { CF_DOMAIN=0, CF_PATH, CF_NAME, CF_HOST,
58  CF_VALUE, CF_EXPIRE, CF_PROVER, CF_SECURE };
59 
60 
61 class CookieRequest {
62 public:
63  DCOPClient *client;
64  DCOPClientTransaction *transaction;
65  TQString url;
66  bool DOM;
67  long windowId;
68 };
69 
70 template class TQPtrList<CookieRequest>;
71 
72 class RequestList : public TQPtrList<CookieRequest>
73 {
74 public:
75  RequestList() : TQPtrList<CookieRequest>() { }
76 };
77 
78 KCookieServer::KCookieServer(const TQCString &name)
79  :KDEDModule(name)
80 {
81  mOldCookieServer = new DCOPClient(); // backwards compatibility.
82  mOldCookieServer->registerAs("kcookiejar", false);
83  mOldCookieServer->setDaemonMode( true );
84  mCookieJar = new KCookieJar;
85  mPendingCookies = new KHttpCookieList;
86  mPendingCookies->setAutoDelete(true);
87  mRequestList = new RequestList;
88  mAdvicePending = false;
89  mTimer = new TQTimer();
90  connect( mTimer, TQT_SIGNAL( timeout()), TQT_SLOT( slotSave()));
91  mConfig = new TDEConfig("kcookiejarrc");
92  mCookieJar->loadConfig( mConfig );
93 
94  TQString filename = locateLocal("data", "kcookiejar/cookies");
95 
96  // Stay backwards compatible!
97  TQString filenameOld = locate("data", "kfm/cookies");
98  if (!filenameOld.isEmpty())
99  {
100  mCookieJar->loadCookies( filenameOld );
101  if (mCookieJar->saveCookies( filename))
102  {
103  unlink(TQFile::encodeName(filenameOld)); // Remove old kfm cookie file
104  }
105  }
106  else
107  {
108  mCookieJar->loadCookies( filename);
109  }
110  connect(this, TQT_SIGNAL(windowUnregistered(long)),
111  this, TQT_SLOT(slotDeleteSessionCookies(long)));
112 }
113 
114 KCookieServer::~KCookieServer()
115 {
116  if (mCookieJar->changed())
117  slotSave();
118  delete mOldCookieServer;
119  delete mCookieJar;
120  delete mTimer;
121  delete mPendingCookies;
122  delete mConfig;
123 }
124 
125 bool KCookieServer::cookiesPending( const TQString &url, KHttpCookieList *cookieList )
126 {
127  TQString fqdn;
128  TQStringList domains;
129  TQString path;
130  // Check whether 'url' has cookies on the pending list
131  if (mPendingCookies->isEmpty())
132  return false;
133  if (!KCookieJar::parseURL(url, fqdn, path))
134  return false;
135 
136  mCookieJar->extractDomains( fqdn, domains );
137  for( KHttpCookie *cookie = mPendingCookies->first();
138  cookie != 0L;
139  cookie = mPendingCookies->next())
140  {
141  if (cookie->match( fqdn, domains, path))
142  {
143  if (!cookieList)
144  return true;
145  cookieList->append(cookie);
146  }
147  }
148  if (!cookieList)
149  return false;
150  return cookieList->isEmpty();
151 }
152 
153 void KCookieServer::addCookies( const TQString &url, const TQCString &cookieHeader,
154  long windowId, bool useDOMFormat )
155 {
156  KHttpCookieList cookieList;
157  if (useDOMFormat)
158  cookieList = mCookieJar->makeDOMCookies(url, cookieHeader, windowId);
159  else
160  cookieList = mCookieJar->makeCookies(url, cookieHeader, windowId);
161 
162  checkCookies(&cookieList);
163 
164  for(KHttpCookiePtr cookie = cookieList.first(); cookie; cookie = cookieList.first())
165  mPendingCookies->append(cookieList.take());
166 
167  if (!mAdvicePending)
168  {
169  mAdvicePending = true;
170  while (!mPendingCookies->isEmpty())
171  {
172  checkCookies(0);
173  }
174  mAdvicePending = false;
175  }
176 }
177 
178 void KCookieServer::checkCookies( KHttpCookieList *cookieList)
179 {
180  KHttpCookieList *list;
181 
182  if (cookieList)
183  list = cookieList;
184  else
185  list = mPendingCookies;
186 
187  KHttpCookiePtr cookie = list->first();
188  while (cookie)
189  {
190  kdDebug(7104) << "checkCookies: Asking cookie advice for " << cookie->host() << endl;
191  KCookieAdvice advice = mCookieJar->cookieAdvice(cookie);
192  switch(advice)
193  {
194  case KCookieAccept:
195  list->take();
196  mCookieJar->addCookie(cookie);
197  cookie = list->current();
198  break;
199 
200  case KCookieReject:
201  list->take();
202  delete cookie;
203  cookie = list->current();
204  break;
205 
206  default:
207  cookie = list->next();
208  break;
209  }
210  }
211 
212  if (cookieList || list->isEmpty())
213  return;
214 
215  KHttpCookiePtr currentCookie = mPendingCookies->first();
216 
217  KHttpCookieList currentList;
218  currentList.append(currentCookie);
219  TQString currentHost = currentCookie->host();
220 
221  cookie = mPendingCookies->next();
222  while (cookie)
223  {
224  if (cookie->host() == currentHost)
225  {
226  currentList.append(cookie);
227  }
228  cookie = mPendingCookies->next();
229  }
230 
231  KCookieWin *kw = new KCookieWin( 0L, currentList,
232  mCookieJar->preferredDefaultPolicy(),
233  mCookieJar->showCookieDetails() );
234  KCookieAdvice userAdvice = kw->advice(mCookieJar, currentCookie);
235  delete kw;
236  // Save the cookie config if it has changed
237  mCookieJar->saveConfig( mConfig );
238 
239  // Apply the user's choice to all cookies that are currently
240  // queued for this host.
241  cookie = mPendingCookies->first();
242  while (cookie)
243  {
244  if (cookie->host() == currentHost)
245  {
246  switch(userAdvice)
247  {
248  case KCookieAccept:
249  mPendingCookies->take();
250  mCookieJar->addCookie(cookie);
251  cookie = mPendingCookies->current();
252  break;
253 
254  case KCookieReject:
255  mPendingCookies->take();
256  delete cookie;
257  cookie = mPendingCookies->current();
258  break;
259 
260  default:
261  tqWarning(__FILE__":%d Problem!", __LINE__);
262  cookie = mPendingCookies->next();
263  break;
264  }
265  }
266  else
267  {
268  cookie = mPendingCookies->next();
269  }
270  }
271 
272 
273  // Check if we can handle any request
274  for ( CookieRequest *request = mRequestList->first(); request;)
275  {
276  if (!cookiesPending( request->url ))
277  {
278  TQCString replyType;
279  TQByteArray replyData;
280  TQString res = mCookieJar->findCookies( request->url, request->DOM, request->windowId );
281 
282  TQDataStream stream2(replyData, IO_WriteOnly);
283  stream2 << res;
284  replyType = "TQString";
285  request->client->endTransaction( request->transaction,
286  replyType, replyData);
287  CookieRequest *tmp = request;
288  request = mRequestList->next();
289  mRequestList->removeRef( tmp );
290  delete tmp;
291  }
292  else
293  {
294  request = mRequestList->next();
295  }
296  }
297  if (mCookieJar->changed())
298  saveCookieJar();
299 }
300 
301 void KCookieServer::slotSave()
302 {
303  TQString filename = locateLocal("data", "kcookiejar/cookies");
304  mCookieJar->saveCookies(filename);
305 }
306 
307 void KCookieServer::saveCookieJar()
308 {
309  if( mTimer->isActive() )
310  return;
311 
312  mTimer->start( 1000*60*SAVE_DELAY, true );
313 }
314 
315 void KCookieServer::putCookie( TQStringList& out, KHttpCookie *cookie,
316  const TQValueList<int>& fields )
317 {
318  TQValueList<int>::ConstIterator i = fields.begin();
319  for ( ; i != fields.end(); ++i )
320  {
321  switch(*i)
322  {
323  case CF_DOMAIN :
324  out << cookie->domain();
325  break;
326  case CF_NAME :
327  out << cookie->name();
328  break;
329  case CF_PATH :
330  out << cookie->path();
331  break;
332  case CF_HOST :
333  out << cookie->host();
334  break;
335  case CF_VALUE :
336  out << cookie->value();
337  break;
338  case CF_EXPIRE :
339  out << TQString::number(cookie->expireDate());
340  break;
341  case CF_PROVER :
342  out << TQString::number(cookie->protocolVersion());
343  break;
344  case CF_SECURE :
345  out << TQString::number( cookie->isSecure() ? 1 : 0 );
346  break;
347  default :
348  out << TQString::null;
349  }
350  }
351 }
352 
353 bool KCookieServer::cookieMatches( KHttpCookiePtr c,
354  TQString domain, TQString fqdn,
355  TQString path, TQString name )
356 {
357  if( c )
358  {
359  bool hasDomain = !domain.isEmpty();
360  return
361  ((hasDomain && c->domain() == domain) ||
362  fqdn == c->host()) &&
363  (c->path() == path) &&
364  (c->name() == name) &&
365  (!c->isExpired(time(0)));
366  }
367  return false;
368 }
369 
370 // DCOP function
371 TQString
372 KCookieServer::findCookies(TQString url)
373 {
374  return findCookies(url, 0);
375 }
376 
377 // DCOP function
378 TQString
379 KCookieServer::findCookies(TQString url, long windowId)
380 {
381  if (cookiesPending(url))
382  {
383  CookieRequest *request = new CookieRequest;
384  request->client = callingDcopClient();
385  request->transaction = request->client->beginTransaction();
386  request->url = url;
387  request->DOM = false;
388  request->windowId = windowId;
389  mRequestList->append( request );
390  return TQString::null; // Talk to you later :-)
391  }
392 
393  TQString cookies = mCookieJar->findCookies(url, false, windowId);
394 
395  if (mCookieJar->changed())
396  saveCookieJar();
397 
398  return cookies;
399 }
400 
401 // DCOP function
402 TQStringList
403 KCookieServer::findDomains()
404 {
405  TQStringList result;
406  const TQStringList domains = mCookieJar->getDomainList();
407  for ( TQStringList::ConstIterator domIt = domains.begin();
408  domIt != domains.end(); ++domIt )
409  {
410  // Ignore domains that have policy set for but contain
411  // no cookies whatsoever...
412  const KHttpCookieList* list = mCookieJar->getCookieList(*domIt, "");
413  if ( list && !list->isEmpty() )
414  result << *domIt;
415  }
416  return result;
417 }
418 
419 // DCOP function
420 TQStringList
421 KCookieServer::findCookies(TQValueList<int> fields,
422  TQString domain,
423  TQString fqdn,
424  TQString path,
425  TQString name)
426 {
427  TQStringList result;
428  bool allDomCookies = name.isEmpty();
429 
430  const KHttpCookieList* list = mCookieJar->getCookieList(domain, fqdn);
431  if ( list && !list->isEmpty() )
432  {
433  TQPtrListIterator<KHttpCookie>it( *list );
434  for ( ; it.current(); ++it )
435  {
436  if ( !allDomCookies )
437  {
438  if ( cookieMatches(it.current(), domain, fqdn, path, name) )
439  {
440  putCookie(result, it.current(), fields);
441  break;
442  }
443  }
444  else
445  putCookie(result, it.current(), fields);
446  }
447  }
448  return result;
449 }
450 
451 // DCOP function
452 TQString
453 KCookieServer::findDOMCookies(TQString url)
454 {
455  return findDOMCookies(url, 0);
456 }
457 
458 // DCOP function
459 TQString
460 KCookieServer::findDOMCookies(TQString url, long windowId)
461 {
462  // We don't wait for pending cookies because it locks up konqueror
463  // which can cause a deadlock if it happens to have a popup-menu up.
464  // Instead we just return pending cookies as if they had been accepted already.
465  KHttpCookieList pendingCookies;
466  cookiesPending(url, &pendingCookies);
467 
468  return mCookieJar->findCookies(url, true, windowId, &pendingCookies);
469 }
470 
471 // DCOP function
472 void
473 KCookieServer::addCookies(TQString arg1, TQCString arg2, long arg3)
474 {
475  addCookies(arg1, arg2, arg3, false);
476 }
477 
478 // DCOP function
479 void
480 KCookieServer::deleteCookie(TQString domain, TQString fqdn,
481  TQString path, TQString name)
482 {
483  const KHttpCookieList* list = mCookieJar->getCookieList( domain, fqdn );
484  if ( list && !list->isEmpty() )
485  {
486  TQPtrListIterator<KHttpCookie>it (*list);
487  for ( ; it.current(); ++it )
488  {
489  if( cookieMatches(it.current(), domain, fqdn, path, name) )
490  {
491  mCookieJar->eatCookie( it.current() );
492  saveCookieJar();
493  break;
494  }
495  }
496  }
497 }
498 
499 // DCOP function
500 void
501 KCookieServer::deleteCookiesFromDomain(TQString domain)
502 {
503  mCookieJar->eatCookiesForDomain(domain);
504  saveCookieJar();
505 }
506 
507 
508 // Qt function
509 void
510 KCookieServer::slotDeleteSessionCookies( long windowId )
511 {
512  deleteSessionCookies(windowId);
513 }
514 
515 // DCOP function
516 void
517 KCookieServer::deleteSessionCookies( long windowId )
518 {
519  mCookieJar->eatSessionCookies( windowId );
520  saveCookieJar();
521 }
522 
523 void
524 KCookieServer::deleteSessionCookiesFor(TQString fqdn, long windowId)
525 {
526  mCookieJar->eatSessionCookies( fqdn, windowId );
527  saveCookieJar();
528 }
529 
530 // DCOP function
531 void
532 KCookieServer::deleteAllCookies()
533 {
534  mCookieJar->eatAllCookies();
535  saveCookieJar();
536 }
537 
538 // DCOP function
539 void
540 KCookieServer::addDOMCookies(TQString arg1, TQCString arg2, long arg3)
541 {
542  addCookies(arg1, arg2, arg3, true);
543 }
544 
545 // DCOP function
546 void
547 KCookieServer::setDomainAdvice(TQString url, TQString advice)
548 {
549  TQString fqdn;
550  TQString dummy;
551  if (KCookieJar::parseURL(url, fqdn, dummy))
552  {
553  TQStringList domains;
554  mCookieJar->extractDomains(fqdn, domains);
555 
556  mCookieJar->setDomainAdvice(domains[domains.count() > 3 ? 3 : 0],
557  KCookieJar::strToAdvice(advice));
558  // Save the cookie config if it has changed
559  mCookieJar->saveConfig( mConfig );
560  }
561 }
562 
563 // DCOP function
564 TQString
565 KCookieServer::getDomainAdvice(TQString url)
566 {
567  KCookieAdvice advice = KCookieDunno;
568  TQString fqdn;
569  TQString dummy;
570  if (KCookieJar::parseURL(url, fqdn, dummy))
571  {
572  TQStringList domains;
573  mCookieJar->extractDomains(fqdn, domains);
574 
575  TQStringList::ConstIterator it = domains.begin();
576  while ( (advice == KCookieDunno) && (it != domains.end()) )
577  {
578  // Always check advice in both ".domain" and "domain". Note
579  // that we only want to check "domain" if it matches the
580  // fqdn of the requested URL.
581  if ( (*it)[0] == '.' || (*it) == fqdn )
582  advice = mCookieJar->getDomainAdvice(*it);
583  ++it;
584  }
585  if (advice == KCookieDunno)
586  advice = mCookieJar->getGlobalAdvice();
587  }
588  return KCookieJar::adviceToStr(advice);
589 }
590 
591 // DCOP function
592 void
593 KCookieServer::reloadPolicy()
594 {
595  mCookieJar->loadConfig( mConfig, true );
596 }
597 
598 // DCOP function
599 void
600 KCookieServer::shutdown()
601 {
602  deleteLater();
603 }
604 
605 #include "kcookieserver.moc"
606 

tdeioslave/http

Skip menu "tdeioslave/http"
  • Main Page
  • Alphabetical List
  • Class List
  • File List

tdeioslave/http

Skip menu "tdeioslave/http"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeioslave/http by doxygen 1.8.8
This website is maintained by Timothy Pearson.