Atrinik Server  4.0
http_server.c
Go to the documentation of this file.
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
33 #include <global.h>
34 #include <http_server.h>
35 #include <toolkit/process.h>
36 #include <toolkit/curl.h>
37 #include <toolkit/packet.h>
38 
39 TOOLKIT_API(DEPENDS(process), IMPORTS(logger));
40 
44 static curl_request_t *current_request;
45 
49 static pthread_mutex_t request_lock;
50 
52 static void
53 http_data_cb (process_t *process, uint8_t *data, size_t len)
54 {
55  char buf[HUGE_BUF * 4];
56  size_t pos = 0;
57  while (packet_to_string(data, len, &pos, VS(buf)) != NULL) {
58  LOG(HTTP, "%s", buf);
59  }
60 }
61 
63 static void
64 http_curl_cb (curl_request_t *request, void *user_data)
65 {
66  pthread_mutex_lock(&request_lock);
67  current_request = NULL;
68  pthread_mutex_unlock(&request_lock);
69 
70  if (curl_request_get_state(request) != CURL_STATE_OK) {
71  int code = curl_request_get_http_code(request);
72  LOG(ERROR,
73  "Failed to connect to local HTTP server at %s; ensure the HTTP "
74  "server is running and accessible (HTTP code: %d).",
76  code);
77  }
78 
79  curl_request_free(request);
80 }
81 
85 TOOLKIT_INIT_FUNC(http_server)
86 {
87  if (settings.http_server) {
88  process_t *process = process_create("python");
89  process_add_arg(process, "tools/http_server.py");
90  process_set_data_out_cb(process, http_data_cb);
91  process_set_data_err_cb(process, http_data_cb);
92  process_set_restart(process, true);
93 
94  if (!process_start(process)) {
95  LOG(ERROR, "Failed to start the HTTP server process.");
96  exit(EXIT_FAILURE);
97  }
98 
99  process_check(process);
100 
101  if (!process_is_running(process)) {
102  LOG(ERROR,
103  "Failed to start up the HTTP server; please consult the "
104  "README file.");
105  }
106  }
107 
108  pthread_mutex_init(&request_lock, NULL);
109 
110  pthread_mutex_lock(&request_lock);
111  /* Verify the HTTP server is running. */
112  current_request = curl_request_create(settings.http_url,
113  CURL_PKEY_TRUST_APPLICATION);
114  curl_request_set_cb(current_request, http_curl_cb, NULL);
115  curl_request_set_delay(current_request, 100000);
116  curl_request_start_get(current_request);
117  pthread_mutex_unlock(&request_lock);
118 }
119 TOOLKIT_INIT_FUNC_FINISH
120 
125 {
126  pthread_mutex_lock(&request_lock);
127  if (current_request != NULL) {
128  pthread_mutex_unlock(&request_lock);
129  curl_state_t state;
130  do {
131  pthread_mutex_lock(&request_lock);
132  if (current_request == NULL) {
133  pthread_mutex_unlock(&request_lock);
134  break;
135  }
136  state = curl_request_get_state(current_request);
137  pthread_mutex_unlock(&request_lock);
138  sleep(1);
139  } while (state == CURL_STATE_INPROGRESS);
140 
141  /* No other thread is working with the current request at this
142  * point. */
143  if (current_request != NULL) {
144  curl_request_free(current_request);
145  current_request = NULL;
146  }
147  } else {
148  pthread_mutex_unlock(&request_lock);
149  }
150 
151  pthread_mutex_destroy(&request_lock);
152 }
153 TOOLKIT_DEINIT_FUNC_FINISH
static curl_request_t * current_request
Definition: http_server.c:44
char http_url[MAX_BUF]
Definition: global.h:459
static pthread_mutex_t request_lock
Definition: http_server.c:49
bool http_server
Definition: global.h:454
static void http_data_cb(process_t *process, uint8_t *data, size_t len)
Definition: http_server.c:53
union @21 data
Data about the rule.
TOOLKIT_INIT_FUNC(http_server)
Definition: http_server.c:85
struct settings_struct settings
Definition: init.c:55
TOOLKIT_INIT_FUNC_FINISH TOOLKIT_DEINIT_FUNC(http_server)
Definition: http_server.c:124
static void http_curl_cb(curl_request_t *request, void *user_data)
Definition: http_server.c:64