int discard_request(ne_socket *sock) { char buffer[1024]; size_t offset = want_header?strlen(want_header):0; clength = 0; NE_DEBUG(NE_DBG_HTTP, "Discarding request...\n"); do { ONV(ne_sock_readline(sock, buffer, 1024) < 0, ("error reading line: %s", ne_sock_error(sock))); NE_DEBUG(NE_DBG_HTTP, "[req] %s", buffer); if (strncasecmp(buffer, "content-length:", 15) == 0) { clength = atoi(buffer + 16); } if (got_header != NULL && want_header != NULL && strncasecmp(buffer, want_header, offset) == 0 && buffer[offset] == ':') { char *value = buffer + offset + 1; if (*value == ' ') value++; got_header(ne_shave(value, "\r\n")); } } while (strcmp(buffer, "\r\n") != 0); return OK; }
static int expect100(void) { ne_socket *sock = ne_sock_create(); char req[BUFSIZ], buf[BUFSIZ]; ne_status status = {0}; const ne_inet_addr *ia; int success = 0; if (strcmp(ne_get_scheme(i_session), "https") == 0) { t_context("skipping for SSL server"); return SKIP; } for (ia = ne_addr_first(i_address); ia && !success; ia = ne_addr_next(i_address)) success = ne_sock_connect(sock, ia, i_port) == 0; ONN("could not connect to server", !success); sprintf(req, "PUT %sexpect100 HTTP/1.1" EOL "Host: %s" EOL "Content-Length: 100" EOL "Expect: 100-continue" EOL EOL, i_path, ne_get_server_hostport(i_session)); NE_DEBUG(NE_DBG_SOCKET, "Request:\n%s", req); ONS("sending request", ne_sock_fullwrite(sock, req, strlen(req))); switch (ne_sock_block(sock, 30)) { case NE_SOCK_TIMEOUT: ONN("timeout waiting for interim response", FAIL); break; case 0: /* no error. */ break; default: ONN("error reading from socket", FAIL); break; } ONS("reading status line", ne_sock_readline(sock, buf, BUFSIZ)); NE_DEBUG(NE_DBG_HTTP, "[status] %s", buf); ONN("parse status line", ne_parse_statusline(buf, &status)); if (status.code == 100) { char rbuf[100] = {0}; ONN("write request body", ne_sock_fullwrite(sock, rbuf, 100)); } ne_sock_close(sock); return OK; }
static int line_expect(ne_socket *sock, const char *line) { ssize_t ret = ne_sock_readline(sock, buffer, BUFSIZ); size_t len = strlen(line); NE_DEBUG(NE_DBG_SOCKET, " -> expected=%s -> actual=%s", line, buffer); ONV(ret == NE_SOCK_CLOSED, ("socket closed, expecting `%s'", line)); ONV(ret < 0, ("socket error `%s', expecting `%s'", ne_sock_error(sock), line)); ONV((size_t)ret != len || strcmp(line, buffer), ("readline mismatch: `%s' not `%s'", buffer, line)); return OK; }
/* echoes back lines. */ static int echo_server(ne_socket *sock, void *ud) { ssize_t ret; while ((ret = ne_sock_readline(sock, buffer, sizeof(buffer))) > 0) { NE_DEBUG(NE_DBG_SOCKET, "Line: %s", buffer); ONN("write failed", ne_sock_fullwrite(sock, buffer, ret)); NE_DEBUG(NE_DBG_SOCKET, "Wrote line.\n"); } ONN("readline failed", ret != NE_SOCK_CLOSED); return 0; }
static int line_toolong(void) { ne_socket *sock; ssize_t ret; DECL(oneline, "AAAAAA\n"); CALL(begin(&sock, serve_sstring, &oneline)); ret = ne_sock_readline(sock, buffer, 5); ONV(ret != NE_SOCK_ERROR, ("readline should fail on long line: %" NE_FMT_SSIZE_T, ret)); reap_server(); ne_sock_close(sock); return OK; }
static int line_overflow(void) { ne_socket *sock; ssize_t ret; DECL_LONG(line, 'A', OVERLEN); CALL(begin(&sock, serve_sstring, &line)); PEEK("A"); /* fill the read buffer */ ret = ne_sock_readline(sock, buffer, OVERLEN); ONV(ret != NE_SOCK_ERROR, ("readline should fail on overlong line: %" NE_FMT_SSIZE_T, ret)); ne_free(line.data); return finish(sock, 0); }
static int line_closure(void) { ne_socket *sock; ssize_t ret; DECL(oneline, STR "\n" "foobar"); CALL(begin(&sock, serve_sstring, &oneline)); LINE(STR "\n"); ret = ne_sock_readline(sock, buffer, BUFSIZ); ONV(ret != NE_SOCK_CLOSED, ("readline got %" NE_FMT_SSIZE_T " not EOF: %s", ret, ne_sock_error(sock))); return finish(sock, 0); }
static int line_long_chunked(void) { ne_socket *sock; ssize_t ret; DECL_LONG(line, 'Z', OVERLEN); CALL(begin(&sock, serve_sstring_slowly, &line)); FULLREAD("ZZZZZZZZ"); /* fill the buffer */ ret = ne_sock_readline(sock, buffer, sizeof buffer); ONV(ret != NE_SOCK_ERROR, ("readline gave %" NE_FMT_SSIZE_T " not failure", ret)); reap_server(); ne_sock_close(sock); ne_free(line.data); return OK; }
static int try_prebind(int addr, int port) { ne_socket *sock = ne_sock_create(); ne_inet_addr *ia; char buf[128], line[256]; unsigned int srvport; ia = ne_iaddr_make(ne_iaddr_ipv4, raw_127); ONN("ne_iaddr_make returned NULL", ia == NULL); CALL(new_spawn_server(1, serve_ppeer, NULL, &srvport)); ne_sock_prebind(sock, addr ? ia : NULL, port ? 7778 : 0); ONN("could not connect", ne_sock_connect(sock, ia, srvport)); ne_snprintf(line, sizeof line, "%s@%d\n", ne_iaddr_print(ia, buf, sizeof buf), 7778); if (!port) { /* Don't know what port will be chosen, so... */ ssize_t ret = ne_sock_readline(sock, buffer, BUFSIZ); ONV(ret < 0, ("socket error `%s'", ne_sock_error(sock))); ONV(strncmp(line, buffer, strchr(line, '@') - line) != 0, ("bad address: '%s', expecting '%s'", buffer, line)); } else { LINE(line); } ne_sock_close(sock); CALL(await_server()); ne_iaddr_free(ia); return OK; }
static int readline_timeout(void) { TO_BEGIN; TO_OP(ne_sock_readline(sock, buffer, 1)); TO_FINISH; }