1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
//
// This file is part of the aMule Project.
//
// Copyright (c) 2003-2011 Angel Vidal ( kry@amule.org )
// Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
// Copyright (c) 2002-2011 Merkur ( devs@emule-project.net / http://www.emule-project.net )
//
// Any parts of this program derived from the xMule, lMule or eMule project,
// or contributed by third-party developers are copyrighted by their
// respective authors.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
//

#ifndef WEBSERVER_H
#define WEBSERVER_H

#include "config.h"		// Needed for ASIO_SOCKETS

#ifdef WITH_LIBPNG
	#include <png.h>
#endif

#include "WebInterface.h"
#include <map>				// Needed for std::map
#include <set>				// Needed for std::set
#include "RLE.h"
#include "OtherStructs.h"
#include <ec/cpp/ECID.h>	// Needed for CECID

#ifdef ENABLE_UPNP
#	include "UPnPBase.h"
#endif

#include <wx/datetime.h>	// For DownloadFile::wxtLastSeenComplete

#ifdef _MSC_VER
#define strncasecmp _strnicmp
#define snprintf sprintf_s
#define atoll _atoi64
#define strdup _strdup
#endif

class CWebSocket;
class CMD4Hash;

#define SESSION_TIMEOUT_SECS	300	// 5 minutes session expiration
#define SHORT_FILENAME_LENGTH	40	// Max size of file name.

wxString _SpecialChars(wxString str);

class CEC_PartFile_Tag;
class CEC_SharedFile_Tag;
class CEC_UpDownClient_Tag;
class CEC_SearchFile_Tag;
class CProgressImage;
class CEC_KadNode_Tag;

class CURLDecoder
{
      public:
	static wxString	Decode(const wxString& url);
};

class DownloadFile : public CECID {
	public:
		wxString	sFileName;
		uint8		nFileStatus;
		uint64		lFileSize;
		uint64		lFileCompleted;
		uint64		lFileTransferred;
		unsigned long	lFileSpeed;
		long		lSourceCount;
		long		lNotCurrentSourceCount;
		long		lTransferringSourceCount;
		long		lSourceCountA4AF;
		double		fCompleted;
		uint32		lFilePrio;
		bool		bFileAutoPriority;
		wxString	sFileHash;
		wxString	sED2kLink;
		uint8		nCat;
		wxDateTime	wxtLastSeenComplete;

		CMD4Hash	nHash;

		CProgressImage *m_Image;
		PartFileEncoderData m_Encoder;
		ArrayOfUInts16 m_PartInfo;
		std::vector<Gap_Struct> m_ReqParts;
		ArrayOfUInts64	m_Gaps;

		// container require this
		static class DownloadFileInfo *GetContainerInstance();
		DownloadFile(CEC_PartFile_Tag *);
		void ProcessUpdate(CEC_PartFile_Tag *);
		uint32 ID() { return ECID(); }
};

class SharedFile : public CECID {
	public:
		wxString	sFileName;
		uint64		lFileSize;
		uint64		nFileTransferred;
		uint64		nFileAllTimeTransferred;
		uint16		nFileRequests;
		uint32		nFileAllTimeRequests;
		uint16		nFileAccepts;
		uint32		nFileAllTimeAccepts;
		uint8		nFilePriority;
		bool		bFileAutoPriority;
		wxString	sFileHash;
		wxString	sED2kLink;

		CMD4Hash	nHash;

		static class SharedFileInfo *GetContainerInstance();
		SharedFile(CEC_SharedFile_Tag *);
		void ProcessUpdate(CEC_SharedFile_Tag *);
		uint32 ID() { return ECID(); }
};

class ServerEntry {
	public:
		wxString	sServerName;
		wxString	sServerDescription;
		uint32		nServerIP;
		uint16		nServerPort;
		wxString	sServerIP;
		int		nServerUsers;
		int		nServerMaxUsers;
		int		nServerFiles;

		static class ServersInfo *GetContainerInstance();
		uint32 ID() { return nServerIP; }
};

// This is a client we are uploading to, not a file
class UploadFile : public CECID {
	public:
		wxString  sUserName;
		uint32 nTransferredUp;
		uint32 nTransferredDown;
		uint32 nSpeed;
		uint32 nUploadFile;		// ECID of shared file uploading to client

		UploadFile(CEC_UpDownClient_Tag *tag);

		static class UploadsInfo *GetContainerInstance();
		uint32 ID() { return ECID(); }
};

class SearchFile : public CECID {
	public:
		wxString sFileName;
		uint64 lFileSize;
		CMD4Hash  nHash;
		wxString  sHash;
		long lSourceCount;
		bool bPresent;

		SearchFile(CEC_SearchFile_Tag *);

		void ProcessUpdate(CEC_SearchFile_Tag *);
		static class SearchInfo *GetContainerInstance();
		uint32 ID() { return ECID(); }
};


/*!
 * T - type of items in container
 */
template <class T>
class ItemsContainer {
	protected:
		CamulewebApp *m_webApp;
		std::list<T> m_items;


		void EraseAll()
		{
			m_items.erase(m_items.begin(), m_items.end());
		}
	public:
		ItemsContainer(CamulewebApp *webApp)
		{
			m_webApp = webApp;
		}
		virtual ~ItemsContainer() { }


		int ItemCount()
		{
			return m_items.size();
		}


		T *AddItem(T &item)<--- Parent function 'ItemsContainer < SharedFile >::AddItem'<--- Parent function 'ItemsContainer < SearchFile >::AddItem'<--- Parent function 'ItemsContainer < DownloadFile >::AddItem'<--- Parameter 'item' can be declared as reference to const
		{
			m_items.push_back(item);
			T *real_ptr = &(m_items.back());
			return real_ptr;
		}

		/*!
		 * Re-query server: refresh all dataset
		 */
		virtual bool ReQuery() = 0;<--- Virtual function in base class<--- Virtual function in base class<--- Virtual function in base class<--- Virtual function in base class<--- Virtual function in base class

		typedef typename std::list<T>::iterator ItemIterator;
		ItemIterator GetBeginIterator()
		{
			return m_items.begin();
		}
		ItemIterator GetEndIterator()
		{
			return m_items.end();
		}
};

/*!
 * T - type of items in container
 * I - type of item ID
 * G - type of tag in EC
 */
template <class T, class G, class I>
class UpdatableItemsContainer : public ItemsContainer<T> {
	protected:
		// need duplicate list with a map, so check "do we already have"
		// will take O(log(n)) instead of O(n)
		// map will contain pointers to items in list
		std::map<I, T *> m_items_hash;
	public:
		UpdatableItemsContainer(CamulewebApp *webApp) : ItemsContainer<T>(webApp)
		{
		}

		T *AddItem(T &item)<--- Derived function 'UpdatableItemsContainer < SharedFile , CEC_SharedFile_Tag , uint32 >::AddItem'<--- Derived function 'UpdatableItemsContainer < SearchFile , CEC_SearchFile_Tag , uint32 >::AddItem'<--- Derived function 'UpdatableItemsContainer < DownloadFile , CEC_PartFile_Tag , uint32 >::AddItem'
		{
			T *real_ptr = ItemsContainer<T>::AddItem(item);
			m_items_hash[item.ID()] = real_ptr;
			return real_ptr;
		}

		T *GetByID(I id)
		{
			// avoid creating nodes
			return m_items_hash.count(id) ? m_items_hash[id] : NULL;
		}

		T * GetByHash(const CMD4Hash &fileHash)
		{
			T * ret = 0;
			for (typename std::map<I, T *>::iterator it = m_items_hash.begin(); it != m_items_hash.end(); ++it) {
				if (it->second->nHash == fileHash) {<--- Consider using std::find_if algorithm instead of a raw loop.
					ret = it->second;
					break;
				}
			}
			return ret;
		}

		/*!
		 * Process answer of update request, create list of new items for
		 * full request later. Also remove items that no longer exist in core
		 */
		void ProcessUpdate(const CECPacket *reply, CECPacket *full_req, int req_type)
		{
			std::set<I> core_files;
			for (CECPacket::const_iterator it = reply->begin(); it != reply->end(); ++it) {
				G *tag = (G *) & *it;

				core_files.insert(tag->ID());
				if ( m_items_hash.count(tag->ID()) ) {
					T *item = m_items_hash[tag->ID()];
					item->ProcessUpdate(tag);
				} else {
					full_req->AddTag(CECTag(req_type, tag->ID()));
				}
			}
			std::list<I> del_ids;
			for(typename std::list<T>::iterator j = this->m_items.begin(); j != this->m_items.end(); ++j) {
				if ( core_files.count(j->ID()) == 0 ) {
					// item may contain data that need to be freed externally, before
					// dtor is called and memory freed

					T *real_ptr = &*j;
					this->ItemDeleted(real_ptr);

					del_ids.push_back(j->ID());
				}
			}
			for(typename std::list<I>::iterator j = del_ids.begin(); j != del_ids.end(); ++j) {
				m_items_hash.erase(*j);
				for(typename std::list<T>::iterator k = this->m_items.begin(); k != this->m_items.end(); ++k) {
					if ( *j == k->ID() ) {
						this->m_items.erase(k);
						break;
					}
				}
			}
		}

		void ProcessFull(const CECPacket *reply)
		{
			for (CECPacket::const_iterator it = reply->begin(); it != reply->end(); ++it) {
				G *tag = (G *) & *it;
				// initialize item data from EC tag
				T item(tag);
				T *real_ptr = AddItem(item);
				// initialize any external data that may depend on this item
				this->ItemInserted(real_ptr);
			}
		}

		bool DoRequery(int cmd, int tag)
		{
			CECPacket req_sts(cmd, EC_DETAIL_UPDATE);

			//
			// Phase 1: request status
			const CECPacket *reply = this->m_webApp->SendRecvMsg_v2(&req_sts);
			if ( !reply ) {
				return false;
			}

			//
			// Phase 2: update status, mark new files for subsequent query
			CECPacket req_full(cmd);

			ProcessUpdate(reply, &req_full, tag);

			delete reply;

			// Phase 3: request full info about files we don't have yet
			if ( req_full.HasChildTags() ) {
				reply = this->m_webApp->SendRecvMsg_v2(&req_full);
				if ( !reply ) {
					return false;
				}
				ProcessFull(reply);
				delete reply;
			}
			return true;
		}

		virtual void ItemDeleted(T *) { }<--- Virtual function in base class
		virtual void ItemInserted(T *) { }<--- Virtual function in base class
};

class UploadsInfo : public ItemsContainer<UploadFile> {
	public:
		// can be only one instance.
		static UploadsInfo *m_This;

		UploadsInfo(CamulewebApp *webApp);

		virtual bool ReQuery();<--- Function in derived class
};

class ServersInfo : public ItemsContainer<ServerEntry> {
	public:
		// can be only one instance.
		static ServersInfo *m_This;

		ServersInfo(CamulewebApp *webApp);

		virtual bool ReQuery();<--- Function in derived class

};


class SharedFileInfo : public UpdatableItemsContainer<SharedFile, CEC_SharedFile_Tag, uint32> {
	public:
		// can be only one instance.
		static SharedFileInfo *m_This;

		SharedFileInfo(CamulewebApp *webApp);

		virtual bool ReQuery();<--- Function in derived class

};

class SearchInfo : public UpdatableItemsContainer<SearchFile, CEC_SearchFile_Tag, uint32> {
	public:
		static SearchInfo *m_This;

		SearchInfo(CamulewebApp *webApp);

		virtual bool ReQuery();<--- Function in derived class

};


class CImageLib;
class DownloadFileInfo : public UpdatableItemsContainer<DownloadFile, CEC_PartFile_Tag, uint32> {
		CImageLib *m_ImageLib;

		// parameters of progress images
		wxString m_Template;
		int m_width, m_height;
	public:
		// can be only one instance.
		static DownloadFileInfo *m_This;

		DownloadFileInfo(CamulewebApp *webApp, CImageLib *imlib);

		void LoadImageParams(wxString &tpl, int width, int height);

		virtual bool ReQuery();<--- Function in derived class

		// container requirements
		void ItemInserted(DownloadFile *item);<--- Function in derived class
		void ItemDeleted(DownloadFile *item);<--- Function in derived class
};

class CAnyImage {
	protected:
		unsigned char *m_data;<--- Parent variable 'CAnyImage::m_data'

		int m_width, m_height;
		wxString m_name;

		int m_size, m_alloc_size;
		wxString m_Http;

		void Realloc(int size);

		void SetHttpType(wxString ext);
	public:
		CAnyImage(int size);
		CAnyImage(int width, int height);
		virtual ~CAnyImage();<--- Virtual destructor in base class<--- Virtual destructor in base class

		const wxString& GetHTTP() const { return m_Http; }

		virtual unsigned char *RequestData(int &size);<--- Virtual function in base class<--- Virtual function in base class
};

class CFileImage : public virtual CAnyImage {
	public:
		CFileImage(const wxString& name);

		bool OpenOk() { return m_size != 0; }
};

class CImage3D_Modifiers {
		unsigned char *m_modifiers;
		int m_width;
	public:
		CImage3D_Modifiers(int width);
		~CImage3D_Modifiers();

		unsigned char operator[](int i)
		{
			return (i < m_width) ? m_modifiers[i] : 0;
		}
};

class CProgressImage : public virtual CAnyImage {
	protected:
		DownloadFile *m_file;

		wxString m_template;

		//
		// Turn list of gaps, partstatus into array of color strips
		typedef struct Color_Gap_Struct : public Gap_Struct {
			uint32 color;
		} Color_Gap_Struct;

		// result of rendering - single line
		uint32 *m_ColorLine;
		void CreateSpan();
	public:
		CProgressImage(int w, int h, wxString &tmpl, DownloadFile *file);

		~CProgressImage();<--- Destructor in derived class<--- Virtual destructor in base class

		const wxString &Name() { return m_name; }

		virtual wxString GetHTML() = 0;<--- Virtual function in base class<--- Virtual function in base class
};

#ifdef WITH_LIBPNG

//
// Dynamic png image generation
//
class CDynPngImage : public virtual CAnyImage {

	public:
		CDynPngImage(int w, int h);
		~CDynPngImage();<--- Destructor in derived class<--- Virtual destructor in base class

		virtual unsigned char *RequestData(int &size);<--- Function in derived class<--- Virtual function in base class

	protected:
		png_bytep m_img_data;
		png_bytep *m_row_ptrs;

		static void png_write_fn(png_structp png_ptr, png_bytep data, png_size_t length);

};

//
// Dynamic png image generation from gap info
class CDynProgressImage : public virtual CProgressImage, public virtual CDynPngImage {
		CImage3D_Modifiers m_modifiers;

		void DrawImage();
	public:
		CDynProgressImage(int w, int h,	wxString &tmpl, DownloadFile *file);
		~CDynProgressImage();<--- Destructor in derived class

		virtual unsigned char *RequestData(int &size);<--- Function in derived class
		virtual wxString GetHTML();<--- Function in derived class
};

#else


//
// Fallback to original implementation
class CDynProgressImage : public virtual CProgressImage {
	public:
		CDynProgressImage(int w, int h,	wxString &tmpl, DownloadFile *file);

		virtual wxString GetHTML();<--- Function in derived class
};

#endif

//
// Representing statistical sample for some parameter. Circular buffer
// inside to avoid rellocations
//
class CStatsData {
		uint32 *m_data;
		uint32 m_max_value;
		int m_size;
		int m_start_index, m_end_index, m_curr_index;
	public:
		CStatsData(int size);
		~CStatsData();

		int Size() const { return m_size; }
		uint32 Max() const { return m_max_value; }
		uint32 GetFirst();
		uint32 GetNext();

		void PushSample(uint32 sample);
};

class CStatsCollection {
		CStatsData *m_down_speed, *m_up_speed,
			*m_conn_number, *m_kad_count;

		CamulewebApp *m_iface;
		double m_LastTimeStamp;
		int m_size;
	public:
		CStatsCollection(int size, CamulewebApp	*iface);
		~CStatsCollection();

		CStatsData *DownloadSpeed() { return m_down_speed; }
		CStatsData *UploadSpeed() { return m_up_speed; }
		CStatsData *ConnCount() { return m_conn_number; }
		CStatsData *KadCount() { return m_kad_count; }

		void ReQuery();
};

#ifdef WITH_LIBPNG

//
// This gonna to represent data used to "write" numbers on
// dynamically generated images.
// Easiest way to represt numbers: 7-segments model
//
class CNumImageMask {
		png_bytep *m_row_mask_ptrs;
		int m_width, m_height;
		int m_v_segsize, m_h_segsize;

		// mask generation
		void DrawHorzLine(int off);
		void DrawVertLine(int offx, int offy);
		void DrawSegment(int id);

		static const int m_num_to_7_decode[10];
	public:
		CNumImageMask(int number, int width, int height);
		~CNumImageMask();

		void Apply(png_bytep *image, int offx, int offy);
};

class CDynStatisticImage : public virtual CDynPngImage {
		CStatsData *m_data;<--- Derived variable 'CDynStatisticImage::m_data'

		// size of "font" of imprinted numbers
		int m_num_font_w_size, m_num_font_h_size;

		int m_left_margin, m_bottom_margin;
		int m_y_axis_size;

		// hope nobody needs "define" for 10 !
		CNumImageMask *m_digits[10];

		// indicates whether data should be divided on 1024 before
		// drawing graph.
		bool m_scale1024;

		//
		// Prepared background
		//
		png_bytep m_background;
		png_bytep *m_row_bg_ptrs;

		void DrawImage();
	public:
		CDynStatisticImage(int height, bool scale1024, CStatsData *data);
		~CDynStatisticImage();<--- Destructor in derived class

		virtual unsigned char *RequestData(int &size);<--- Function in derived class
		virtual wxString GetHTML();
};

#endif

class CImageLib {
		typedef std::map<wxString, CAnyImage *> ImageMap;
		ImageMap m_image_map;
		wxString m_image_dir;
	public:
		CImageLib(wxString image_dir);
		~CImageLib();

		CAnyImage *GetImage(const wxString &name);
		void AddImage(CAnyImage *img, const wxString &name);
		void RemoveImage(const wxString &name);
};

class CParsedUrl {
		wxString m_path, m_file;
		std::map<wxString, wxString> m_params;
	public:
		CParsedUrl(const wxString &url);

		const wxString &Path() { return m_path; }
		const wxString &File() { return m_file; }

		const wxString &Param(const wxString &key)
		{
			return m_params[key];
		}

		void ConvertParams(std::map<std::string, std::string> &);
};

// Changing this to a typedef struct{} makes egcs compiler do it all wrong and crash on run
struct ThreadData {
	CParsedUrl	parsedURL;
	wxString	sURL;
	int		SessionID;
	CWebSocket	*pSocket;
};

#ifndef ASIO_SOCKETS
enum {
    // Socket handlers
    ID_WEBLISTENSOCKET_EVENT = wxID_HIGHEST+123,  // random safe ID
    ID_WEBCLIENTSOCKET_EVENT,
};
#endif

#ifdef ENABLE_UPNP
class CUPnPControlPoint;
class CUPnPPortMapping;
#endif

class CWebLibSocketServer : public CLibSocketServer {
public:
	CWebLibSocketServer(const class amuleIPV4Address& adr, int flags, CWebServerBase * webServerBase);

	virtual	void OnAccept();
private:
	CWebServerBase * m_webServerBase;
};

class CWebServerBase : public wxEvtHandler {
	protected:
		CWebLibSocketServer *m_webserver_socket;

		ServersInfo m_ServersInfo;
		SharedFileInfo m_SharedFileInfo;
		DownloadFileInfo m_DownloadFileInfo;
		UploadsInfo m_UploadsInfo;
		SearchInfo m_SearchInfo;

		CStatsCollection m_Stats;

		CImageLib m_ImageLib;

		virtual void ProcessURL(ThreadData) = 0;<--- Virtual function in base class
		virtual void ProcessImgFileReq(ThreadData) = 0;<--- Virtual function in base class

		int GzipCompress(Bytef *dest, uLongf *destLen,
			const Bytef *source, uLong sourceLen, int level);

		friend class CWebSocket;
		friend class CPhPLibContext;

		bool m_upnpEnabled;
		int m_upnpTCPPort;
#ifdef ENABLE_UPNP
		CUPnPControlPoint *m_upnp;
		std::vector<CUPnPPortMapping> m_upnpMappings;
#endif
#ifdef ASIO_SOCKETS
		CAsioService *m_AsioService;
#else
		void OnWebSocketServerEvent(wxSocketEvent& event);
		void OnWebSocketEvent(wxSocketEvent& event);
		DECLARE_EVENT_TABLE();
#endif
	public:
		CWebServerBase(CamulewebApp *webApp, const wxString& templateDir);
		virtual ~CWebServerBase();<--- Virtual destructor in base class

		void Send_Discard_V2_Request(CECPacket *request);

		void StartServer();
		void StopServer();

		void Print(const wxString &s);

		long GetWSPrefs();

		//
		// Command interface
		//
		void Send_ReloadSharedFile_Cmd();

		void Send_SharedFile_Cmd(wxString file_hash, wxString cmd, uint32 opt_arg = 0);
		void Send_DownloadFile_Cmd(wxString file_hash, wxString cmd, uint32 opt_arg = 0);

		void Send_DownloadSearchFile_Cmd(wxString file_hash, uint8 cat);

		void Send_Server_Cmd(uint32 ip, uint16 port, wxString cmd);
		void Send_AddServer_Cmd(wxString addr, wxString port, wxString name);

		void Send_Search_Cmd(wxString search, wxString extention, wxString type,
			EC_SEARCH_TYPE search_type, uint32 avail, uint32 min_size, uint32 max_size);

		bool Send_DownloadEd2k_Cmd(wxString link, uint8 cat);

		void Reload_Stats()
		{
			m_Stats.ReQuery();
		}

		CamulewebApp	*webInterface;

};

class CSession {
	public:
		bool m_loggedin;
		time_t m_last_access;
		std::map<std::string, std::string> m_vars, m_get_vars;

		void LoadVars(CParsedUrl &url);
};

/*
 * Script based webserver
 */
class CScriptWebServer : public CWebServerBase {
		wxString m_wwwroot;
		wxString m_index;

		char *ProcessHtmlRequest(const char *filename, long &size);
		char *ProcessPhpRequest(const char *filename, CSession *sess, long &size);

		char *GetErrorPage(const char *message, long &size);
		char *Get_404_Page(long &size);

		std::map<int, CSession> m_sessions;

		CSession *CheckLoggedin(ThreadData &);
	protected:
		virtual void ProcessURL(ThreadData);<--- Function in derived class<--- Virtual function in base class
		virtual void ProcessImgFileReq(ThreadData);<--- Function in derived class
	public:
		CScriptWebServer(CamulewebApp *webApp, const wxString& templateDir);
		~CScriptWebServer();<--- Destructor in derived class<--- Virtual destructor in base class

};

class CNoTemplateWebServer : public CScriptWebServer {
	protected:
		virtual void ProcessURL(ThreadData);<--- Function in derived class
	public:
		CNoTemplateWebServer(CamulewebApp *webApp);
		~CNoTemplateWebServer();<--- Destructor in derived class
};

#endif // WEBSERVER_H
// File_checked_for_headers