1 /*
2 
3   Copyright (c) 2018 Martin Sustrik
4 
5   Permission is hereby granted, free of charge, to any person obtaining a copy
6   of this software and associated documentation files (the "Software"),
7   to deal in the Software without restriction, including without limitation
8   the rights to use, copy, modify, merge, publish, distribute, sublicense,
9   and/or sell copies of the Software, and to permit persons to whom
10   the Software is furnished to do so, subject to the following conditions:
11 
12   The above copyright notice and this permission notice shall be included
13   in all copies or substantial portions of the Software.
14 
15   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21   IN THE SOFTWARE.
22 
23 */
24 
25 #include <string.h>
26 
27 #include "assert.h"
28 #include "../libdill.h"
29 
nohttp_client(int s)30 coroutine void nohttp_client(int s) {
31     s = ws_attach_client(s, WS_NOHTTP | WS_TEXT, NULL, NULL, -1);
32     errno_assert(s >= 0);
33     int rc = msend(s, "ABC", 3, -1);
34     errno_assert(rc == 0);
35     char buf[3];
36     ssize_t sz = mrecv(s, buf, sizeof(buf), -1);
37     errno_assert(sz >= 0);
38     assert(sz == 3);
39     assert(buf[0] == 'D' && buf[1] == 'E' && buf[2] == 'F');
40     sz = mrecv(s, buf, sizeof(buf), -1);
41     assert(sz < 0 && errno == EPIPE);
42     int status;
43     sz = ws_status(s, &status, buf, sizeof(buf));
44     errno_assert(sz >= 0);
45     assert(status == 1000);
46     assert(sz == 2);
47     assert(buf[0] == 'O' && buf[1] == 'K');
48     s = ws_detach(s, 0, NULL, 0, -1);
49     errno_assert(s >= 0);
50     rc = hclose(s);
51     errno_assert(rc == 0);
52 }
53 
http_client(int s)54 coroutine void http_client(int s) {
55     s = ws_attach_client(s, WS_BINARY, "/", "www.example.org", -1);
56     errno_assert(s >= 0);
57     int rc = msend(s, "ABC", 3, -1);
58     errno_assert(rc == 0);
59     char buf[3];
60     ssize_t sz = mrecv(s, buf, sizeof(buf), -1);
61     errno_assert(sz >= 0);
62     assert(sz == 3);
63     assert(buf[0] == 'D' && buf[1] == 'E' && buf[2] == 'F');
64     sz = mrecv(s, buf, sizeof(buf), -1);
65     assert(sz < 0 && errno == EPIPE);
66     int status;
67     sz = ws_status(s, &status, buf, sizeof(buf));
68     errno_assert(sz >= 0);
69     assert(status == 1000);
70     assert(sz == 0);
71     s = ws_detach(s, 0, NULL, 0, -1);
72     errno_assert(s >= 0);
73     rc = hclose(s);
74     errno_assert(rc == 0);
75 }
76 
main(void)77 int main(void) {
78     int p[2];
79     int rc = ipc_pair(p);
80     errno_assert(rc == 0);
81     int cr = go(nohttp_client(p[1]));
82     errno_assert(cr >= 0);
83     int s = ws_attach_server(p[0], WS_NOHTTP | WS_TEXT, NULL, 0, NULL, 0, -1);
84     errno_assert(s >= 0);
85     char buf[3];
86     ssize_t sz = mrecv(s, buf, sizeof(buf), -1);
87     errno_assert(sz >= 0);
88     assert(sz == 3);
89     assert(buf[0] == 'A' && buf[1] == 'B' && buf[2] == 'C');
90     rc = msend(s, "DEF", 3, -1);
91     errno_assert(rc == 0);
92     s = ws_detach(s, 1000, "OK", 2, -1);
93     errno_assert(s >= 0);
94     rc = hclose(s);
95     errno_assert(rc == 0);
96     rc = hclose(cr);
97     errno_assert(rc == 0);
98 
99     rc = ipc_pair(p);
100     errno_assert(rc == 0);
101     cr = go(http_client(p[1]));
102     errno_assert(cr >= 0);
103     char resource[256];
104     char host[256];
105     s = ws_attach_server(p[0], WS_BINARY, resource, sizeof(resource),
106         host, sizeof(host), -1);
107     errno_assert(s >= 0);
108     assert(strcmp(resource, "/") == 0);
109     assert(strcmp(host, "www.example.org") == 0);
110     sz = mrecv(s, buf, sizeof(buf), -1);
111     errno_assert(sz >= 0);
112     assert(sz == 3);
113     assert(buf[0] == 'A' && buf[1] == 'B' && buf[2] == 'C');
114     rc = msend(s, "DEF", 3, -1);
115     errno_assert(rc == 0);
116     s = ws_detach(s, 1000, NULL, 0, -1);
117     errno_assert(s >= 0);
118     rc = hclose(s);
119     errno_assert(rc == 0);
120     rc = hclose(cr);
121     errno_assert(rc == 0);
122 
123     return 0;
124 }
125 
126