src/server/test/testutils.c

Sat, 30 Mar 2024 12:35:09 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 30 Mar 2024 12:35:09 +0100
changeset 513
9a49c245a49c
parent 498
0d80f8a2b29f
permissions
-rw-r--r--

change net_write to attempt to write all bytes, improve error handling

211
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2019 Olaf Wintermann. All rights reserved.
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
498
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
31 #include <limits.h>
211
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 321
diff changeset
33 #include <cx/string.h>
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 321
diff changeset
34 #include <cx/utils.h>
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 321
diff changeset
35 #include <cx/printf.h>
211
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 #include "../util/pblock.h"
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38
232
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
39 #include "../util/io.h"
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
40
211
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 #include "testutils.h"
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 Session* testutil_session(void) {
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 pool_handle_t *pool = pool_create();
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 NSAPISession *sn = nsapisession_create(pool);
233
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
46 sn->connection = testutil_dummy_connection(pool);
211
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 return &sn->sn;
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 }
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 Request* testutil_request(pool_handle_t *pool, const char *method, const char *uri) {
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 NSAPIRequest *rq = pool_malloc(pool, sizeof(NSAPIRequest));
321
9bf375c4b5bd fix test request memory initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 251
diff changeset
53 ZERO(rq, sizeof(NSAPIRequest));
211
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 HTTPRequest httprequest;
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 ZERO(&httprequest, sizeof(HTTPRequest));
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 request_initialize(pool, &httprequest, rq);
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 321
diff changeset
59 cxmutstr clf = cx_asprintf("%s %s HTTP/1.1", method, uri);
211
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 pblock_kvinsert(
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 pb_key_clf_request,
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 clf.ptr,
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 clf.length,
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 rq->rq.reqpb);
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 free(clf.ptr);
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 pblock_nvinsert(
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 "method",
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 method,
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 rq->rq.reqpb);
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 pblock_nvinsert(
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 "protocol",
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 "HTTP/1.1",
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 rq->rq.reqpb);
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 pblock_nvinsert("uri", uri, rq->rq.reqpb);
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 return &rq->rq;
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 }
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81
233
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
82 static int dummyconn_read(Connection *conn, void *buf, int len) {
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
83 return len;
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
84 }
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
85
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
86 static int dummyconn_write(Connection *conn, const void *buf, int len) {
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
87 return len;
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
88 }
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
89
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
90 static void dummyconn_close(Connection *conn) {
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
91
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
92 }
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
93
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
94
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
95 Connection* testutil_dummy_connection(pool_handle_t *pool) {
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
96 Connection *conn = pool_malloc(pool, sizeof(Connection));
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
97 ZERO(conn, sizeof(Connection));
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
98 conn->read = dummyconn_read;
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
99 conn->write = dummyconn_write;
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
100 conn->close = dummyconn_close;
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
101 return conn;
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
102 }
c5985d2fc19a add test for webdav_propfind() that checks if the response is valid xml
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 232
diff changeset
103
211
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 void testutil_request_body(Session *sn, Request *rq, const char *body, size_t len) {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 321
diff changeset
105 cxmutstr cl = cx_asprintf("%d", (int)len);
211
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 pblock_nvreplace("content-length", cl.ptr, rq->headers);
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 free(cl.ptr);
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 netbuf *inbuf = pool_malloc(sn->pool, sizeof(netbuf));
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 inbuf->sd = NULL;
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 inbuf->inbuf = pool_malloc(sn->pool, len);
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 inbuf->pos = 0;
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 inbuf->maxsize = len;
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 inbuf->cursize = len;
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 sn->inbuf = inbuf;
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 memcpy(inbuf->inbuf, body, len);
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 }
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 void testutil_destroy_session(Session *sn) {
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 pool_destroy(sn->pool);
2160585200ac add propfind/proppatch parser and first iteration of the new webdav api
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 }
232
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
123
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
124
498
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
125 static ssize_t test_io_write(IOStream *io, const void *buf, size_t size) {
232
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
126 TestIOStream *st = (TestIOStream*)io;
498
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
127 if(size > st->max_write) size = st->max_write;
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 321
diff changeset
128 return cxBufferWrite(buf, 1, size, st->buf);
232
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
129 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
130
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
131 static ssize_t test_io_writev(IOStream *io, struct iovec *iovec, int iovctn) {
498
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
132 TestIOStream *st = (TestIOStream*)io;
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
133 ssize_t wv = 0;
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
134 for(int i=0;i<iovctn;i++) {
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
135 ssize_t available = st->max_write - wv;
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
136 size_t len = iovec[i].iov_len;
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
137 if(len > available) {
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
138 len = available;
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
139 }
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
140
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
141 ssize_t w = test_io_write(io, iovec[i].iov_base, len);
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
142 if(w <= 0) {
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
143 break;
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
144 }
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
145 wv += w;
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
146 if(wv >= st->max_write) {
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
147 break;
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
148 }
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
149 }
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
150 return wv;
232
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
151 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
152
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
153 static ssize_t test_io_read(IOStream *io, void *buf, size_t size) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
154 return -1;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
155 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
156
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
157 static void test_io_close(IOStream *io) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
158
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
159 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
160
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
161 static void test_io_finish(IOStream *io) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
162
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
163 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
164
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
165 static void test_io_setmode(IOStream *io, int mode) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
166
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
167 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
168
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
169 static int test_io_poll(IOStream *io, EventHandler *ev, int events , Event *event) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
170 return 1;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
171 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
172
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
173 TestIOStream* testutil_iostream(size_t size, int autoextend) {
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
174 TestIOStream *stream = calloc(1, sizeof(TestIOStream));
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
175 int flags = 0;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
176 if(autoextend) {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 321
diff changeset
177 flags = CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS;
232
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
178 }
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 321
diff changeset
179 stream->buf = malloc(sizeof(CxBuffer));
498
0d80f8a2b29f fix net_http_write when used with chunked transfer encoding and non-blocking IO
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
180 stream->max_write = INT_MAX;
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 321
diff changeset
181 cxBufferInit(stream->buf, NULL, size, cxDefaultAllocator, flags);
232
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
182
251
f727a21497bb add basic PUT implementation and tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 233
diff changeset
183 stream->io.st.write = test_io_write;
f727a21497bb add basic PUT implementation and tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 233
diff changeset
184 stream->io.st.writev = test_io_writev;
f727a21497bb add basic PUT implementation and tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 233
diff changeset
185 stream->io.st.close = test_io_close;
f727a21497bb add basic PUT implementation and tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 233
diff changeset
186 stream->io.st.finish = test_io_finish;
513
9a49c245a49c change net_write to attempt to write all bytes, improve error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 498
diff changeset
187 stream->io.st.type = IO_STREAM_TYPE_HTTP;
232
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
188
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
189 return stream;
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
190 }
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
191
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
192 void testutil_iostream_destroy(TestIOStream *stream) {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 321
diff changeset
193 cxBufferDestroy(stream->buf);
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 321
diff changeset
194 free(stream->buf);
232
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
195 free(stream);
499711b2a970 add xml writer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 211
diff changeset
196 }

mercurial