/* * Copyright (C) 2005-2009 Andre Noll * * Licensed under the GPL v2. For licencing details see COPYING. */ /** \file http_send.c paraslash's http sender */ #include #include #include "para.h" #include "error.h" #include "string.h" #include "server.cmdline.h" #include "afh.h" #include "afs.h" #include "server.h" #include "http.h" #include "vss.h" #include "list.h" #include "send.h" #include "close_on_fork.h" #include "net.h" #include "fd.h" #include "chunk_queue.h" #include "acl.h" /** Message sent to clients that do not send a valid get request. */ #define HTTP_ERR_MSG "HTTP/1.0 400 Bad Request\n" /** The possible states of a client from the server's POV. */ enum http_client_status { /** We accepted the connection on the tcp socket. */ HTTP_CONNECTED, /** Successfully received the get request. */ HTTP_GOT_GET_REQUEST, /** Connection is ready for sending audio data. */ HTTP_STREAMING, /** We didn't receive a valid get request. */ HTTP_INVALID_GET_REQUEST }; /** For each connected client, a structure of this type is maintained. */ struct private_http_sender_data { /** The current state of this client. */ enum http_client_status status; }; static struct sender_status http_sender_status, *hss = &http_sender_status; static int http_send_msg(struct sender_client *sc, const char *msg) { int ret = send_buffer(sc->fd, msg); if (ret < 0) shutdown_client(sc, hss); return ret; } static void http_send_ok_msg(struct sender_client *sc) { PARA_INFO_LOG("sending http ok message to fd %d\n", sc->fd); http_send_msg(sc, HTTP_OK_MSG); } static int http_send_err_msg(struct sender_client *sc) { PARA_NOTICE_LOG("sending bad request message to fd %d\n", sc->fd); return http_send_msg(sc, HTTP_ERR_MSG); } static void http_shutdown_clients(void) { shutdown_clients(hss); } static void http_send(long unsigned current_chunk, __a_unused long unsigned chunks_sent, const char *buf, size_t len, const char *header_buf, size_t header_len) { struct sender_client *sc, *tmp; list_for_each_entry_safe(sc, tmp, &hss->client_list, node) { struct private_http_sender_data *phsd = sc->private_data; if (phsd->status != HTTP_STREAMING) continue; send_chunk(sc, hss, 0, current_chunk, buf, len, header_buf, header_len); } } static void http_post_select(fd_set *rfds, __a_unused fd_set *wfds) { struct sender_client *sc, *tmp; struct private_http_sender_data *phsd; if (hss->listen_fd < 0) return; list_for_each_entry_safe(sc, tmp, &hss->client_list, node) { phsd = sc->private_data; switch (phsd->status) { case HTTP_STREAMING: /* nothing to do */ break; case HTTP_CONNECTED: /* need to recv get request */ if (FD_ISSET(sc->fd, rfds)) { if (recv_pattern(sc->fd, HTTP_GET_MSG, MAXLINE) < 0) { phsd->status = HTTP_INVALID_GET_REQUEST; } else { phsd->status = HTTP_GOT_GET_REQUEST; PARA_INFO_LOG("received get request\n"); } } break; case HTTP_GOT_GET_REQUEST: /* need to send ok msg */ phsd->status = HTTP_STREAMING; http_send_ok_msg(sc); break; case HTTP_INVALID_GET_REQUEST: /* need to send err msg */ if (http_send_err_msg(sc) >= 0) shutdown_client(sc, hss); break; } } if (!FD_ISSET(hss->listen_fd, rfds)) return; sc = accept_sender_client(hss); if (!sc) return; phsd = para_malloc(sizeof(*phsd)); sc->private_data = phsd; phsd->status = HTTP_CONNECTED; } static void http_pre_select(int *max_fileno, fd_set *rfds, fd_set *wfds) { struct sender_client *sc, *tmp; if (hss->listen_fd < 0) return; para_fd_set(hss->listen_fd, rfds, max_fileno); list_for_each_entry_safe(sc, tmp, &hss->client_list, node) { struct private_http_sender_data *phsd = sc->private_data; if (phsd->status == HTTP_CONNECTED) /* need to recv get request */ para_fd_set(sc->fd, rfds, max_fileno); if (phsd->status == HTTP_GOT_GET_REQUEST || phsd->status == HTTP_INVALID_GET_REQUEST) para_fd_set(sc->fd, wfds, max_fileno); } } static int http_com_on(__a_unused struct sender_command_data *scd) { return generic_com_on(hss, IPPROTO_TCP); } static int http_com_off(__a_unused struct sender_command_data *scd) { generic_com_off(hss); return 1; } static int http_com_deny(struct sender_command_data *scd) { generic_com_deny(scd, hss); return 1; } static int http_com_allow(struct sender_command_data *scd) { generic_com_allow(scd, hss); return 1; } static char *http_info(void) { return get_sender_info(hss, "http"); } /** * The init function of the http sender. * * \param s Pointer to the http sender struct. * * It initializes all function pointers of \a s, the client list and the access * control list. If the autostart option was given, the tcp port is opened. */ void http_send_init(struct sender *s) { int ret; s->info = http_info; s->send = http_send; s->pre_select = http_pre_select; s->post_select = http_post_select; s->shutdown_clients = http_shutdown_clients; s->help = generic_sender_help; s->client_cmds[SENDER_ON] = http_com_on; s->client_cmds[SENDER_OFF] = http_com_off; s->client_cmds[SENDER_DENY] = http_com_deny; s->client_cmds[SENDER_ALLOW] = http_com_allow; s->client_cmds[SENDER_ADD] = NULL; s->client_cmds[SENDER_DELETE] = NULL; init_sender_status(hss, conf.http_access_arg, conf.http_access_given, conf.http_port_arg, conf.http_max_clients_arg, conf.http_default_deny_given); if (conf.http_no_autostart_given) return; ret = generic_com_on(hss, IPPROTO_TCP); if (ret < 0) PARA_ERROR_LOG("%s\n", para_strerror(-ret)); }