summaryrefslogtreecommitdiff
path: root/libs/networking/src/HTTPServer.cc
blob: f435e2f60265488ec348abdcc8c43403f2d91b3e (plain)
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
/**
 * ======================== legal notice ======================
 * 
 * File:      HTTPServer.cc
 * Created:   4. Juli 2012, 12:16
 * Author:    <a href="mailto:geronimo013@gmx.de">Geronimo</a>
 * Project:   libnetworking: classes for tcp/ip sockets and http-protocol handling
 * 
 * CMP - compound media player
 * 
 * is a client/server mediaplayer intended to play any media from any workstation
 * without the need to export or mount shares. cmps is an easy to use backend
 * with a (ready to use) HTML-interface. Additionally the backend supports
 * authentication via HTTP-digest authorization.
 * cmpc is a client with vdr-like osd-menues.
 * 
 * Copyright (c) 2012 Reinhard Mantey, some rights reserved!
 * published under Creative Commons by-sa
 * For details see http://creativecommons.org/licenses/by-sa/3.0/
 * 
 * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp
 * 
 * --------------------------------------------------------------
 */
#include <HTTPServer.h>
#include <HTTPResponse.h>
#include <ConnectionHandler.h>
#include <CondWait.h>
#include <Mutex.h>
#include <Logging.h>
#include <util.h>
#include <stdio.h>

static volatile bool running = true;
cHTTPServer::cHTTPServer(cServerConfig &Config)
 : config(Config)
#ifdef LIVE_CLEANUP
 , cleaner(NULL)
#endif
{
  threads = new std::vector<cThread *>();
#ifdef LIVE_CLEANUP
  cleaner = new cThread(cleanerThread, NULL, "cleanup thread stubs");
#endif
}

cHTTPServer::~cHTTPServer()
{
#ifdef LIVE_CLEANUP
  cleaner->Cancel(0);
  delete cleaner;
#else
  Cleanup();
#endif
  delete threads;
}

void cHTTPServer::Action()
{
#ifdef LIVE_CLEANUP
  if (cleaner) {
     fprintf(stderr, "check size of thread pool ...\n");
     if (!threads->empty()) fprintf(stderr, "thread pool has size of %lu\n", threads->size());
     fprintf(stderr, "start cleaner thread\n");
     cleaner->Start();
     }
#endif
  fprintf(stderr, "startup server now\n");
  while (Running()) {
        cConnectionPoint *cp = ServerSocket().Accept();

        if (cp) {
           // this has to come after accept, as the hostnames can not be determined before an established connection.
           if (!cHTTPResponse::ServerID()) {
              char *id;

              asprintf(&id, "HTTPServer %s:%d", ServerSocket().ThisSide()->RealName(), ServerSocket().Port());
              cHTTPResponse::SetServerID(id);
              }
           cConnectionHandler *ch = new cConnectionHandler(*cp, config);

           ch->Start();

#ifdef LIVE_CLEANUP
           cMutexLock poolSync(&poolMutex);
#endif
           threads->push_back(ch);
           }
        }
}

bool cHTTPServer::Running()
{
  return ServerSocket().Active();
}

#ifdef LIVE_CLEANUP
int cHTTPServer::cleanerThread(void *opaque, cThread *ThreadContext)
{
  cHTTPServer *server = (cHTTPServer *)opaque;

  server->Cleanup(ThreadContext);
  return 0;
}

void cHTTPServer::Cleanup(cThread *ThreadContext)
{
  if (!ThreadContext) return;
  cThread *p;

  fprintf(stderr, "cleanup-Thread - gonna start loop ...\n");
  while (ThreadContext->Running()) {
        fprintf(stderr, "check list of possible threads ...\n");
        if (!threads->empty()) {
           fprintf(stderr, "thread pool is not empty ...\n");
           for (size_t i=0; i < threads->size(); ++i) {
               fprintf(stderr, "ok, got first entry to check ...\n");
               p = (*threads)[i];
               if (p->Active()) continue;

               fprintf(stderr, "thread %lu looks dead ...\n", i);
               isyslog("thread %lu is not active, so just clean it up", i);
               p->Cancel();
               delete p;
               cMutexLock poolSync(&poolMutex);
               threads->erase(threads->begin() + i);
               break;
               }
           }
        cCondWait::SleepMs(200);
        }
  ThreadContext->Cancel();
}
#else
void cHTTPServer::Cleanup()
{
  cThread *t;

  for (size_t i=0; i < threads->size(); ++i) {
      t = (*threads)[i];
      t->Cancel();
      delete t;
      }
}
#endif

bool cHTTPServer::Start()
{
  Action();
  return true;
}

void cHTTPServer::Stop()
{
  ServerSocket().SetActive(false);
}