Unnamed Fossil Project

Check-in [54075b1cf9]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:httpx/tcpip added
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:54075b1cf9efcb5a0a671a992a5dcec14526412c
User & Date: alexey@sudachen.name 2012-01-05 16:22:26
Context
2012-01-11
15:05
updated check-in: 9d693e4f86 user: alexey@sudachen.name tags: trunk
2012-01-05
16:22
httpx/tcpip added check-in: 54075b1cf9 user: alexey@sudachen.name tags: trunk
16:18
updated check-in: 766446a1a6 user: alexey@sudachen.name tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added httpx.hc.





































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    http://www.gnu.org/licenses/

Otherwise:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Except as contained in this notice, the name of a copyright holder shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization of the copyright holder.

*/

#ifndef C_once_DAA1D391_392F_467C_B5A8_6C8D0D3A9DCC
#define C_once_DAA1D391_392F_467C_B5A8_6C8D0D3A9DCC

#include "yoyo.hc"
#include "string.hc"
#include "file.hc"
#include "tcpip.hc"

#ifdef _LIBYOYO
#define _YOYO_HTTPX_BUILTIN
#endif

enum
  {
    YOYO_HTTPX_DOING_NOTHING      = 0,
    YOYO_HTTPX_IS_PREPARING       = 1,
    YOYO_HTTPX_IS_RESOLVING       = 2,
    YOYO_HTTPX_IS_CONNECTING      = 3,
    YOYO_HTTPX_IS_QUERYING        = 4,
    YOYO_HTTPX_IS_GETTING_STATUS  = 5,
    YOYO_HTTPX_IS_GETTING_HEADERS = 6,
    YOYO_HTTPX_IS_GETTING_CONTENT = 7,
    YOYO_HTTPX_IS_FINISHED        = 8,
    YOYO_HTTPX_IS_FAILED          = 9,
    YOYO_HTTPX_RESOLVING_ERROR    = 0x1001,
    YOYO_HTTPX_URLPARSING_ERROR   = 0x1002,
    YOYO_HTTPX_GETTING_ERROR      = 0x1003,
  };

typedef struct _YOYO_HTTPX_NTFY
  {
    void (*progress)(struct _YOYO_HTTPX_NTFY *, int st, int count, int total);
    void (*error)(struct _YOYO_HTTPX_NTFY *, int st, char *msg);
  } YOYO_HTTPX_NTFY;

#endif /* C_once_DAA1D391_392F_467C_B5A8_6C8D0D3A9DCC */

Added tcpip.hc.







































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259

/*

Copyright © 2010-2011, Alexéy Sudáchen, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    http://www.gnu.org/licenses/

Otherwise:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Except as contained in this notice, the name of a copyright holder shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization of the copyright holder.

*/

#ifndef C_once_F8F16072_F92E_49E7_A983_54F60965F4C9
#define C_once_F8F16072_F92E_49E7_A983_54F60965F4C9

#include "yoyo.hc"
#include "string.hc"
#include "file.hc"

#ifdef _LIBYOYO
#define _YOYO_TCPIP_BUILTIN
#endif

typedef struct _YOYO_SOCKET
  {
    int       skt;
    char     *host;
    int       port;
    in_addr_t ip;
  } YOYO_SOCKET

#ifdef __windoze
void _WSA_Init()
#ifdef _YOYO_TCPIP_BUILTIN
  {
    static int wsa_status = -1;
    static WSADATA wsa_data = {0};
    if ( wsa_status != 0 )
      {
        if ( 0 != WSAStartup(MAKEWORD(2, 2), &wsa_data) )
          __Raise(YOYO_ERROR_SUBSYSTEM_INIT,"failed to initialize WSA subsystem");
        else
          wsa_status = 0;
      }
    return true;
  }
#endif
  ;
#else
#define _WSA_Init()
#endif


#define Ipv4_Format(Ip) __Pool(Ipv4_Format_Npl(Ip))
char *Ipv4_Format_Npl(in_addr_t ip)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    return __Format_Npl("%d.%d.%d.%d"
              ,(ip&0x0ff)
              ,((ip>>8)&0x0ff)
              ,((ip>>16)&0x0ff)
              ,((ip>>24)&0x0ff));
  }
#endif
  ;
  
in_addr_t Dns_Resolve(char *host)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    in_addr_t ip;
    hostent *hstn = 0;
    
    _WSA_Init();
    if ( !strcmp_I(host,"localhost") ) return 0x0100007f;
    ip = inet_addr(host);
    if ( ip != 0x0ffffffff ) return ip;
  l:
    if ( hstn = gethostbyname(host) )
      {
        memcpy(&ip,hstn->h_addr,Yo_MAX(sizeof(ip),hstn->h_length));
        return ip;
      }
    else
      {
        if ( h_errno == TRY_AGAIN )
          goto l;
        else if ( h_errno == NO_RECOVERY )
          __Raise(YOYO_ERROR_DNS,"unrecoverable DNS error");
        else //( h_errno == HOST_NOT_FOUND ) 
          __Raise_Format(YOYO_ERROR_DNS,("DNS couldn't resolve ip for name %s",host));
      }
    return 0;
  }        
#endif
  ;
  
void Socket_Close(YOYO_SOCKET *sok)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    if ( sok->skt >= 0 ) 
      {
        close(sok->skt);
        sok->skt = -1;
      }
  }
#endif
  ;
  
void YOYO_SOCKET_Destruct(YOYO_SOCKET *sok)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    Socket_Close(sok);
    free(sok->host);
    __Detruct(sok);
  }
#endif
  ;
  
int Socket_Read(YOYO_SOCKET *sok, void *out, int count, int mincount)
#ifdef _YOYO_TCPIP_BUILTIN  
  {
    byte_t *b = out;
    int cc = count;
    while ( cc )
      {
        int q = recv(skt,b,cc,0);
        if ( q < 0 )
          __Raise_Format(YOYO_ERROR_IO,("tcp recv failed with error %s",strerror(errno)));
        STRICT_REQUIRE( q <= cc );
        cc -= q;
        b += q;
        if ( q == 0 && count-cc >= mincount )
          break;
      }
    return count-cc;
  }
#endif
  ;
  
int Socket_Write(YOYO_SOCKET *sok, void *out, int count, int mincount)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    byte_t *b = out;
    int cc = count;
    while ( cc )
      {
        int q = send(skt,b,cc,0);
        if ( q < 0 )
          __Raise_Format(YOYO_ERROR_IO,("tcp send failed with error %s",strerror(errno)));
        STRICT_REQUIRE( q <= cc );
        cc -= q;
        b += q;
        if ( q == 0 && count-cc >= mincount )
          break;
      }
    return count-cc;
  }
#endif
  ;
  
YOYO_SOCKET *Socket_Init(in_addr_t ip, int port, int skt, char *hostname)
#ifdef _YOYO_TCPIP_BUILTIN  
  {
    static YOYO_FUNCTABLE funcs[] = 
      { {0},
        {Oj_Destruct_OjMID,    YOYO_SOCKET_Destruct},
        {Oj_Close_OjMID,       Socket_Close},
        {Oj_Read_OjMID,        Socket_Read},
        {Oj_Write_OjMID,       Socket_Write},
        //{Oj_Available_OjMID,   Socket_Available},
        //{Oj_Eof_OjMID,         Socket_Eof},
        {0}
      };

    YOYO_SOCKET *sok = __Object(sizeof(YOYO_SOCKET),funcs);
    sok->skt = skt;
    sok->port = port;
    sok->ip = ip;
    sok->host = hostname ? Str_Copy_Npl(hostname,-1) : Ipv4_Format_Npl(ip);
  }
#endif
  ;
  
YOYO_TCPSOKET *Socket_Open_Inaddr(in_addr_t ip, int port, char *hostname)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    YOYO_TCPSOKET *sok;
    
    sockaddr_in addr = {0}; 
    int skt, conerr;
    
    _WSA_Init();
    
    addr.sin_family = AF_INET;
    addr.sin_port   = htons(port);
    addr.sin_addr.s_addr = ip;
    
    skt = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
    conerr = 0;
    
    if ( skt < 0 || ( conerr = connect(skt,(sockaddr*)&addr,sizeof(addr)) ) < 0 )
        __Raise_Format(YOYO_ERROR_IO,("tcp connection failed: sok %d, point (%s -> %s):%d, error %d"
                          ,skt
                          ,hostname?hostname:Ipv4_Fomat(ip)
                          ,Ipv4_Fomat(ip)
                          ,port
                          ,conerr));

    sok = Socket_Init(skt,ip,port,hostname);
    return sok;
  }
#endif
  ;
  
YOYO_TCPSOKET *Socket_Open(char *host, int port)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    in_addr_t ip = Dns_Resolve(host);
    YOYO_TCPSOKET *sok = Soket_Open_Inaddr(ip,port,hotname);
    return sok;
  }
#endif
  ;
  
#endif /* C_once_F8F16072_F92E_49E7_A983_54F60965F4C9 */