| 889 | } |
| 890 | |
| 891 | static int handle_connection(HTTPContext *c) |
| 892 | { |
| 893 | int len, ret; |
| 894 | |
| 895 | switch(c->state) { |
| 896 | case HTTPSTATE_WAIT_REQUEST: |
| 897 | case RTSPSTATE_WAIT_REQUEST: |
| 898 | /* timeout ? */ |
| 899 | if ((c->timeout - cur_time) < 0) |
| 900 | return -1; |
| 901 | if (c->poll_entry->revents & (POLLERR | POLLHUP)) |
| 902 | return -1; |
| 903 | |
| 904 | /* no need to read if no events */ |
| 905 | if (!(c->poll_entry->revents & POLLIN)) |
| 906 | return 0; |
| 907 | /* read the data */ |
| 908 | read_loop: |
| 909 | len = recv(c->fd, c->buffer_ptr, 1, 0); |
| 910 | if (len < 0) { |
| 911 | if (ff_neterrno() != FF_NETERROR(EAGAIN) && |
| 912 | ff_neterrno() != FF_NETERROR(EINTR)) |
| 913 | return -1; |
| 914 | } else if (len == 0) { |
| 915 | return -1; |
| 916 | } else { |
| 917 | /* search for end of request. */ |
| 918 | uint8_t *ptr; |
| 919 | c->buffer_ptr += len; |
| 920 | ptr = c->buffer_ptr; |
| 921 | if ((ptr >= c->buffer + 2 && !memcmp(ptr-2, "\n\n", 2)) || |
| 922 | (ptr >= c->buffer + 4 && !memcmp(ptr-4, "\r\n\r\n", 4))) { |
| 923 | /* request found : parse it and reply */ |
| 924 | if (c->state == HTTPSTATE_WAIT_REQUEST) { |
| 925 | ret = http_parse_request(c); |
| 926 | } else { |
| 927 | ret = rtsp_parse_request(c); |
| 928 | } |
| 929 | if (ret < 0) |
| 930 | return -1; |
| 931 | } else if (ptr >= c->buffer_end) { |
| 932 | /* request too long: cannot do anything */ |
| 933 | return -1; |
| 934 | } else goto read_loop; |
| 935 | } |
| 936 | break; |
| 937 | |
| 938 | case HTTPSTATE_SEND_HEADER: |
| 939 | if (c->poll_entry->revents & (POLLERR | POLLHUP)) |
| 940 | return -1; |
| 941 | |
| 942 | /* no need to write if no events */ |
| 943 | if (!(c->poll_entry->revents & POLLOUT)) |
| 944 | return 0; |
| 945 | len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0); |
| 946 | if (len < 0) { |
| 947 | if (ff_neterrno() != FF_NETERROR(EAGAIN) && |
| 948 | ff_neterrno() != FF_NETERROR(EINTR)) { |
no test coverage detected