summaryrefslogtreecommitdiff
path: root/src/libwebvi/request.c
blob: baca4677fefd2db59bb8c5b2918bccd43a830b7f (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
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
#include <glib.h>
#include <string.h>
#include "pipecomponent.h"
#include "webvicontext.h"
#include "request.h"
#include "link.h"

struct WebviRequest {
  PipeComponent *pipe_head;
  gchar *url;
  webvi_callback write_cb;
  webvi_callback read_cb;
  void *writedata;
  void *readdata;
  struct WebviContext *ctx; /* borrowed reference */
};

struct RequestStateMessage {
  RequestState state;
  const char *message;
};

static PipeComponent *pipe_factory(const WebviRequest *self);
static void notify_pipe_finished(RequestState state, void *data);
static const char *pipe_state_to_message(RequestState state);
static gchar *wvt_to_local_file(const WebviContext *ctx, const char *wvt);
static PipeComponent *build_and_start_mainmenu_pipe(const WebviRequest *self);
static PipeComponent *build_and_start_local_pipe(const WebviRequest *self);
static PipeComponent *build_and_start_external_pipe(const WebviRequest *self,
                                                    const char *command);
static PipeComponent *build_and_start_menu_pipe(const WebviRequest *self);

WebviRequest *request_create(const char *url, struct WebviContext *ctx) {
  WebviRequest *req = malloc(sizeof(WebviRequest));
  memset(req, 0, sizeof(WebviRequest));
  req->url = g_strdup(url);
  req->ctx = ctx;
  return req;
}

void request_delete(WebviRequest *self) {
  if (self) {
    request_stop(self);
    pipe_delete_full(self->pipe_head);
    g_free(self->url);
  }
}

gboolean request_start(WebviRequest *self) {
  if (!self->pipe_head) {
    self->pipe_head = pipe_factory(self);
    if (!self->pipe_head) {
      notify_pipe_finished(WEBVISTATE_NOT_FOUND, self);
    }
  }
  return TRUE;
}

void request_stop(WebviRequest *self) {
  // FIXME
}

PipeComponent *pipe_factory(const WebviRequest *self) {
  PipeComponent *head;
  if (strcmp(self->url, "wvt://mainmenu") == 0) {
    head = build_and_start_mainmenu_pipe(self);

  } else if (strncmp(self->url, "wvt://", 6) == 0) {
    head = build_and_start_local_pipe(self);

  } else {
    const LinkAction *action = link_templates_get_action(
      get_link_templates(self->ctx), self->url);
    LinkActionType action_type = action ? link_action_get_type(action) : LINK_ACTION_PARSE;
    if ((action_type == LINK_ACTION_STREAM) || 
        (action_type == LINK_ACTION_EXTERNAL_COMMAND)) {
      const char *command = link_action_get_command(action);
      head = build_and_start_external_pipe(self, command);
    } else {
      head = build_and_start_menu_pipe(self);
    }
  }

  return head;
}

PipeComponent *build_and_start_mainmenu_pipe(const WebviRequest *self) {
  PipeMainMenuDownloader *p1 = pipe_mainmenu_downloader_create(self->ctx);
  PipeComponent *p2 = (PipeComponent *)pipe_callback_wrapper_create(
    self->read_cb, self->readdata, notify_pipe_finished, (void *)self);
  pipe_component_set_next((PipeComponent *)p1, p2);
  pipe_mainmenu_downloader_start(p1);

  return (PipeComponent *)p1;
}

PipeComponent *build_and_start_local_pipe(const WebviRequest *self) {
  gchar *filename = wvt_to_local_file(self->ctx, self->url);
  PipeLocalFile *p1 = pipe_local_file_create(filename);
  g_free(filename);
  PipeComponent *p2 = (PipeComponent *)pipe_callback_wrapper_create(
    self->read_cb, self->readdata, notify_pipe_finished, (void *)self);
  pipe_component_set_next((PipeComponent *)p1, p2);
  pipe_local_file_start(p1);

  return (PipeComponent *)p1;
}

PipeComponent *build_and_start_external_pipe(const WebviRequest *self,
                                             const char *command)
{
  const char *path = webvi_context_get_menu_script_path(self->ctx);
  PipeExternalDownloader *p1 = pipe_external_downloader_create(
    self->url, command, path);
  PipeComponent *p2 = (PipeComponent *)pipe_menu_validator_create();
  PipeComponent *p3 = (PipeComponent *)pipe_callback_wrapper_create(
    self->read_cb, self->readdata, notify_pipe_finished, (void *)self);
  pipe_component_set_next((PipeComponent *)p1, p2);
  pipe_component_set_next((PipeComponent *)p2, p3);
  pipe_external_downloader_start(p1);

  return (PipeComponent *)p1;
}

PipeComponent *build_and_start_menu_pipe(const WebviRequest *self) {
  CURLM *curlmulti = webvi_context_get_curl_multi_handle(self->ctx);
  PipeDownloader *p1 = pipe_downloader_create(self->url, curlmulti);
  PipeComponent *p2 = (PipeComponent *)pipe_link_extractor_create(
    get_link_templates(self->ctx), self->url);
  PipeComponent *p3 = (PipeComponent *)pipe_callback_wrapper_create(
    self->read_cb, self->readdata, notify_pipe_finished, (void *)self);
  pipe_component_set_next((PipeComponent *)p1, p2);
  pipe_component_set_next(p2, p3);
  pipe_downloader_start(p1);

  // TODO: the downloaded HTML should be converted into UTF-8

  return (PipeComponent *)p1;
}

gchar *wvt_to_local_file(const WebviContext *ctx, const char *wvt) {
  if (strncmp(wvt, "wvt://", 6) != 0)
    return NULL;

  const gchar *template_path = webvi_context_get_template_path(ctx);
  if (!template_path)
    return NULL; // FIXME

  // FIXME: .. in paths

  gchar *filename = g_strdup(wvt+6);
  if (filename[0] == '/') {
    // absolute path
    // The path must be located under the template directory
    if (strncmp(filename, template_path, strlen(template_path)) != 0) {
      g_log(LIBWEBVI_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
        "Invalid path in wvt:// url");
      g_free(filename);
      return NULL;
    }
  } else {
    // relative path, concatenate to template_path
    gchar *absolute_path = g_strconcat(template_path, "/", filename, NULL);
    g_free(filename);
    filename = absolute_path;
  }

  return filename;
}

void request_fdset(WebviRequest *self, fd_set *readfds,
                   fd_set *writefds, fd_set *excfds, int *max_fd)
{
  if (self->pipe_head) {
    pipe_fdset(self->pipe_head, readfds, writefds, excfds, max_fd);
  }
}

gboolean request_handle_socket(WebviRequest *self, int sockfd, int ev_bitmask)
{
  if (self->pipe_head) {
    return pipe_handle_socket(self->pipe_head, sockfd, ev_bitmask);
  } else {
    return FALSE;
  }
}

void request_set_write_callback(WebviRequest *self, webvi_callback func) {
  self->write_cb = func;
}

void request_set_read_callback(WebviRequest *self, webvi_callback func) {
  self->read_cb = func;
}

void request_set_write_data(WebviRequest *self, void *data) {
  self->writedata = data;
}

void request_set_read_data(WebviRequest *self, void *data) {
  self->readdata = data;
}

const char *request_get_url(const WebviRequest *self) {
  return self->url;
}

void notify_pipe_finished(RequestState state, void *data) {
  WebviRequest *req = (WebviRequest *)data;
  const char *msg = pipe_state_to_message(state);
  webvi_context_add_finished_message(req->ctx, req, state, msg);
}

const char *pipe_state_to_message(RequestState state) {
  static struct RequestStateMessage messages[] = 
    {{WEBVISTATE_NOT_FINISHED, "Not finished"},
     {WEBVISTATE_FINISHED_OK, "Success"},
     {WEBVISTATE_MEMORY_ALLOCATION_ERROR, "Out of memory"},
     {WEBVISTATE_NOT_FOUND, "Not found"},
     {WEBVISTATE_NETWORK_READ_ERROR, "Failed to receive data from the network"},
     {WEBVISTATE_IO_ERROR, "IO error"},
     {WEBVISTATE_TIMEDOUT, "Timedout"},
     {WEBVISTATE_SUBPROCESS_FAILED, "Failed to execute a subprocess"},
     {WEBVISTATE_INTERNAL_ERROR, "Internal error"}};

  for (int i=0; i<(sizeof(messages)/sizeof(messages[0])); i++) {
    if (state == messages[i].state) {
      return messages[i].message;
    }
  }

  return "Internal error";
}