Unnamed Fossil Project

Check-in [3d1dd3c60f]
Login

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

Overview
Comment:updated
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:3d1dd3c60f88251353ef0e7e74c6e1ece64a1d01
User & Date: alexey@sudachen.name 2012-05-31 03:01:34
Context
2012-06-09
09:51
updated check-in: 81a745c894 user: alexey@sudachen.name tags: trunk
2012-05-31
03:01
updated check-in: 3d1dd3c60f user: alexey@sudachen.name tags: trunk
2012-05-24
03:28
updated check-in: 5dc3de1a25 user: alexey@sudachen.name tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to asio.hc.

49
50
51
52
53
54
55




56
57
58
59
60
61
62
#ifdef _LIBYOYO
#define _YOYO_ASIO_BUILTIN
#endif

#include "yoyo.hc"
#include "datetime.hc"
#include "threads.hc"





#ifdef __windoze
  #include <winsock2.h>
  typedef SOCKET socket_t;
#else
  #include <errno.h>
  #include <sys/types.h>







>
>
>
>







49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifdef _LIBYOYO
#define _YOYO_ASIO_BUILTIN
#endif

#include "yoyo.hc"
#include "datetime.hc"
#include "threads.hc"

#ifdef _MSC_VER
#pragma comment(lib,"ws2_32.lib")
#endif

#ifdef __windoze
  #include <winsock2.h>
  typedef SOCKET socket_t;
#else
  #include <errno.h>
  #include <sys/types.h>

Changes to httpd.hc.

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
...
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
...
274
275
276
277
278
279
280

281

282

283
284
285
286
287
288
289
...
305
306
307
308
309
310
311







312
313
314
315
316
317
318
...
319
320
321
322
323
324
325

326

327

328
329
330
331
332
333
334
...
362
363
364
365
366
367
368

369

370

371
372
373
374
375

376

377

378
379
380
381
382
383
384
...
389
390
391
392
393
394
395

396

397

398
399
400
401
402
403
404
...
435
436
437
438
439
440
441

442

443

444
445
446
447
448
449
450
...
473
474
475
476
477
478
479

480

481
482
483
484
485
486
487
...
505
506
507
508
509
510
511

512
513
514
515
516
517
518
...
580
581
582
583
584
585
586

587

588

589
590
591
592
593
594
595
...
604
605
606
607
608
609
610

611

612

613
614
615
616
617
618
619

620

621

622
623
624

625

626

627
628
629
630
631
632
633

634

635

636
637
638
639
640
641

642

643
644
        
    /* response */
    void *outstrm;
    char *outstrm_type;
    char *outstrm_charset;
    int   outstrm_length;
    

    YOYO_ARRAY *cookie;
    char *location;
    char *cache_control;

    /* internal */    
    YOYO_TCPSOK *sok;
    YOYO_HTTPD  *httpd;
    int nline;
    int left;
    struct _YOYO_HTTPD_REQUEST *next;
  } YOYO_HTTPD_REQUEST;

void Httpd_Detach_Request(YOYO_HTTPD_REQUEST *rqst)

  {
    YOYO_HTTPD *httpd = rqst->httpd;
    YOYO_HTTPD_REQUEST **q = &httpd->rqst;
    while ( *q )
      {
        if ( *q == rqst )
          {
................................................................................
            *q = rqst->next;
            rqst->next = 0;
            break;
          }
        q = &(*q)->next;
      }
  }



void YOYO_HTTPD_REQUEST_Destruct(YOYO_HTTPD_REQUEST *rqst)

  {
    Httpd_Detach_Request(rqst);
    __Unrefe(rqst->httpd);
    __Unrefe(rqst->sok);
    free(rqst->remote_addr);
    free(rqst->uri);
    __Unrefe(rqst->bf);
................................................................................
    __Unrefe(rqst->qhdr);
    __Unrefe(rqst->instrm);
    __Unrefe(rqst->outstrm);
    free(rqst->outstrm_type);
    free(rqst->location);
    free(rqst->cache_control);
    __Unrefe(rqst->cookie);

    free(rqst->outstrm_charset);
    __Destruct(rqst);
  }



enum
  {
    /* status */
    HTTPD_RQST_CONNECT     = 1,
    HTTPD_RQST_POSTDTA     = 2,
    HTTPD_RQST_PERFORM     = 3,

................................................................................
    HTTPD_PUT_METHOD       = 1003,
    HTTPD_HEAD_METHOD      = 1004,
    HTTPD_DELETE_METHOD    = 1005,

  };

char *Httpd_Method_String(int method)

  {
    switch(method)
      {
        case HTTPD_GET_METHOD:    return "GET";
        case HTTPD_POST_METHOD:   return "POST";
        case HTTPD_PUT_METHOD:    return "PUT";
        case HTTPD_HEAD_METHOD:   return "HEAD";
        case HTTPD_DELETE_METHOD: return "DELETE";
      }
      
    return "NONE";
  }



YOYO_HTTPD_REQUEST *Httpd_Request(YOYO_HTTPD *httpd, YOYO_TCPSOK *sok)

  {
    YOYO_HTTPD_REQUEST *rqst = __Object_Dtor(sizeof(YOYO_HTTPD_REQUEST),YOYO_HTTPD_REQUEST_Destruct);
    rqst->httpd = __Refe(httpd);
    rqst->sok = __Refe(sok);
    rqst->remote_addr = Ipv4_Format_Npl(sok->ip);
    rqst->remote_port = sok->port;
    rqst->next = httpd->rqst;
    httpd->rqst = rqst;
    rqst->bf = __Refe(Buffer_Init(0));
    rqst->qhdr = __Refe(Dicto_Ptrs());
    rqst->cookie = __Refe(Array_Pchars());

    Buffer_Reserve(rqst->bf,HTTPD_MAXBUF_LENGTH);
    return rqst;
  }


































char *Httpd_Status_Text(int st)

  {
    switch ( st )
      {
        case 200: return "OK";
        case 201: return "Created";
        case 202: return "Accepted";
        case 203: return "Non-Authoritative Information";
................................................................................
        case 509: return "Bandwidth Limit Exceeded";
        case 510: return "Not Extended";
        case 511: return "Network Authentication Required";
        default:  
          return "Unknown Status"; 
      }
  }



void Httpd_Gen_Response(YOYO_HTTPD_REQUEST *rqst, int st)

  {
    int i;
    YOYO_BUFFER *bf = rqst->bf;
    bf->count = 0;
    Buffer_Printf(bf,"HTTP/1.1 %d %s\r\n",st,Httpd_Status_Text(st));
    Buffer_Append(bf,"Connection: close\r\n",-1);
    if ( rqst->location )
................................................................................
        else
          Buffer_Printf(bf,"Content-Type: %s\r\n",rqst->outstrm_type);
        if ( rqst->outstrm_length )
          Buffer_Printf(bf,"Content-Length: %d\r\n",rqst->outstrm_length);
        else
          Buffer_Append(bf,"Transfer-Encoding: chunked\r\n",-1);
      }







    Buffer_Append(bf,"\r\n",2);
    
    if ( rqst->outstrm )
      {
        rqst->left = rqst->outstrm_length;
        if ( rqst->left <= bf->capacity-bf->count)
          {
................................................................................
            int L = Yo_Minu(rqst->left,bf->capacity-bf->count);
            L = Oj_Read(rqst->outstrm,bf->at+bf->count,L,0);
            rqst->left -= L;
            bf->count += L;
          }
      }
  }  



int Cbk_Httpd_Sending_Result(void *_rqst, int status)

  {
    int L;
    YOYO_HTTPD_REQUEST *rqst = _rqst;
    YOYO_BUFFER *bf = rqst->bf;

    if ( status == ASIO_TIMER )
      return rqst->httpd->callback(rqst->httpd->obj,rqst,HTTPD_STAT_TIMER),0;
................................................................................
        }
      
  finished:
    rqst->httpd->callback(rqst->httpd->obj,rqst,HTTPD_STAT_FINISHED);
    Tcp_Graceful_Close(rqst->sok);
    return 0;
  }



void Httpd_Continue_Request(YOYO_HTTPD_REQUEST *rqst, int st)

  {
    YOYO_BUFFER *bf = rqst->bf;
    Httpd_Gen_Response(rqst,st);
    Tcp_Asio_Send(rqst->sok,bf->at,bf->count,rqst,Cbk_Httpd_Sending_Result);
  }



void Httpd_Perform_Request(YOYO_HTTPD_REQUEST *rqst)

  {
    YOYO_BUFFER *bf = rqst->bf;
    int st;
    
    if ( rqst->method == HTTPD_POST_METHOD || rqst->method == HTTPD_PUT_METHOD )
      {
        if ( !rqst->instrm ) 
................................................................................
        Oj_Seek(rqst->instrm,0,0);
      }
      
    st = rqst->httpd->callback(rqst->httpd->obj,rqst,HTTPD_RQST_PERFORM);
    if ( st )
      Httpd_Continue_Request(rqst,st);
  }

  

int Cbk_Httpd_Getting_Postdta(void *_rqst, int status, int count)

  {
    YOYO_HTTPD_REQUEST *rqst = _rqst;
    YOYO_BUFFER *bf = rqst->bf;
    
    if ( status == ASIO_TIMER )
      return rqst->httpd->callback(rqst->httpd->obj,rqst,HTTPD_STAT_TIMER),0;
    if ( !ASIO_ST_SUCCEEDED(status) )
................................................................................
    else
      {
        Httpd_Perform_Request(rqst);
      }  
    
    return 0;
  }



void Httpd_Analyze_Qline(char *text,YOYO_HTTPD_REQUEST *rqst)

  {
    char *S, *q;
    
    S = text;
    while ( *S && *S != '\n' && Isspace(*S) ) ++S;
    q = S;
    while ( *q && !Isspace(*q) ) ++q;
................................................................................
    while ( *S && Isspace(*S) ) ++S;
    q = S;
    while ( *q && !Isspace(*q) ) ++q;
    
    if ( !strncmp_I(S,"HTTP/1.",7) )
      rqst->httpver = strtol(S+7,0,10);
  }



void Httpd_Analyze_Headers(char *text,YOYO_DICTO *dct)
#ifdef _YOYO_HTTPD_BUILTIN
  {
    int i;
    char *q, *Q, *S = text;
    while (*S!='\n') ++S; /* skip HTTP/1.x query line */
    q = ++S;
................................................................................
        S = ++q;
      }
  }
#endif
  ;

int Cbk_Httpd_Getting_Request(void *_rqst, int status, int count)

  {
    int i;
    YOYO_HTTPD_REQUEST *rqst = _rqst;
    YOYO_BUFFER *bf = rqst->bf;

    if ( status == ASIO_TIMER )
      return rqst->httpd->callback(rqst->httpd->obj,rqst,HTTPD_STAT_TIMER),0;
................................................................................
                     Tcp_Close(rqst->sok),0;

          }
      }

    return 0;
  }



int Cbk_Httpd_Accept(void *_httpd,int status,YOYO_TCPSOK *sok)

  {
    YOYO_HTTPD *httpd = _httpd;

    if ( status == ASIO_TIMER )
      return httpd->callback(httpd->obj,0,HTTPD_STAT_TIMER),0;

    if ( !ASIO_ST_SUCCEEDED(status) )
................................................................................
          Tcp_Asio_Recv(rqst->sok,rqst->bf->at,rqst->bf->capacity,1,rqst,Cbk_Httpd_Getting_Request);
        else
          httpd->callback(httpd->obj,rqst,HTTPD_STAT_REJECTED);
      }

    return 0;
  }



void Httpd_Listen(YOYO_HTTPD *httpd, char *host, int port)

  {
    httpd->sok = __Refe(Tcp_Listen(TCPSOK_ASYNC,host,port,0));
    free(httpd->host);
    httpd->host = Str_Copy_Npl(host,-1);
    httpd->port = port;
    Tcp_Asio_Accept(httpd->sok,httpd,Cbk_Httpd_Accept);
  }



void Http_Shutdown(YOYO_HTTPD *httpd)

  {
    Tcp_Asio_Shutdown(httpd->sok);
  }



void YOYO_HTTPD_Destruct(YOYO_HTTPD *httpd)

  {
    STRICT_REQUIRE(httpd->rqst == 0);
    free(httpd->host);
    __Unrefe(httpd->obj);
    __Unrefe(httpd->sok);
    __Destruct(httpd);
  }



YOYO_HTTPD *Httpd_Server(void *obj, httpd_callback_t callback)

  {
    YOYO_HTTPD *httpd = __Object_Dtor(sizeof(YOYO_HTTPD),YOYO_HTTPD_Destruct);
    httpd->obj  = __Refe(obj);
    httpd->callback = callback;
    return httpd;
  }



#endif /* C_once_BE85262D_CDA8_4AF5_B684_8A30EB6D7370 */








>



|









>







 







>
|
>

>







 







>



>
|
>







 







>












>
|
>

>











>



>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

>







 







>
|
>

>







 







>
>
>
>
>
>
>







 







>
|
>

>







 







>
|
>

>





>
|
>

>







 







>
|
>

>







 







>
|
>

>







 







>
|
>







 







>







 







>
|
>

>







 







>
|
>

>







>
|
>

>



>
|
>

>







>
|
>

>






>
|
>


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
...
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
260
261
262
263
264
265
266
267
268
269
...
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
...
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
...
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
...
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
...
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
...
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
...
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
...
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
...
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
...
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
        
    /* response */
    void *outstrm;
    char *outstrm_type;
    char *outstrm_charset;
    int   outstrm_length;
    
    YOYO_ARRAY *hlns;
    YOYO_ARRAY *cookie;
    char *location;
    char *cache_control;
  
    /* internal */    
    YOYO_TCPSOK *sok;
    YOYO_HTTPD  *httpd;
    int nline;
    int left;
    struct _YOYO_HTTPD_REQUEST *next;
  } YOYO_HTTPD_REQUEST;

void Httpd_Detach_Request(YOYO_HTTPD_REQUEST *rqst)
#ifdef _YOYO_HTTPD_BUILTIN
  {
    YOYO_HTTPD *httpd = rqst->httpd;
    YOYO_HTTPD_REQUEST **q = &httpd->rqst;
    while ( *q )
      {
        if ( *q == rqst )
          {
................................................................................
            *q = rqst->next;
            rqst->next = 0;
            break;
          }
        q = &(*q)->next;
      }
  }
#endif
  ;
  
void YOYO_HTTPD_REQUEST_Destruct(YOYO_HTTPD_REQUEST *rqst)
#ifdef _YOYO_HTTPD_BUILTIN
  {
    Httpd_Detach_Request(rqst);
    __Unrefe(rqst->httpd);
    __Unrefe(rqst->sok);
    free(rqst->remote_addr);
    free(rqst->uri);
    __Unrefe(rqst->bf);
................................................................................
    __Unrefe(rqst->qhdr);
    __Unrefe(rqst->instrm);
    __Unrefe(rqst->outstrm);
    free(rqst->outstrm_type);
    free(rqst->location);
    free(rqst->cache_control);
    __Unrefe(rqst->cookie);
    __Unrefe(rqst->hlns);
    free(rqst->outstrm_charset);
    __Destruct(rqst);
  }
#endif
  ;
  
enum
  {
    /* status */
    HTTPD_RQST_CONNECT     = 1,
    HTTPD_RQST_POSTDTA     = 2,
    HTTPD_RQST_PERFORM     = 3,

................................................................................
    HTTPD_PUT_METHOD       = 1003,
    HTTPD_HEAD_METHOD      = 1004,
    HTTPD_DELETE_METHOD    = 1005,

  };

char *Httpd_Method_String(int method)
#ifdef _YOYO_HTTPD_BUILTIN
  {
    switch(method)
      {
        case HTTPD_GET_METHOD:    return "GET";
        case HTTPD_POST_METHOD:   return "POST";
        case HTTPD_PUT_METHOD:    return "PUT";
        case HTTPD_HEAD_METHOD:   return "HEAD";
        case HTTPD_DELETE_METHOD: return "DELETE";
      }
      
    return "NONE";
  }
#endif
  ;

YOYO_HTTPD_REQUEST *Httpd_Request(YOYO_HTTPD *httpd, YOYO_TCPSOK *sok)
#ifdef _YOYO_HTTPD_BUILTIN
  {
    YOYO_HTTPD_REQUEST *rqst = __Object_Dtor(sizeof(YOYO_HTTPD_REQUEST),YOYO_HTTPD_REQUEST_Destruct);
    rqst->httpd = __Refe(httpd);
    rqst->sok = __Refe(sok);
    rqst->remote_addr = Ipv4_Format_Npl(sok->ip);
    rqst->remote_port = sok->port;
    rqst->next = httpd->rqst;
    httpd->rqst = rqst;
    rqst->bf = __Refe(Buffer_Init(0));
    rqst->qhdr = __Refe(Dicto_Ptrs());
    rqst->cookie = __Refe(Array_Pchars());
    rqst->hlns = __Refe(Array_Pchars());
    Buffer_Reserve(rqst->bf,HTTPD_MAXBUF_LENGTH);
    return rqst;
  }
#endif
  ;
  
int Httpd_Rqst_Range(YOYO_HTTPD_REQUEST *rqst, int *rng_pos, int *rng_len)
#ifdef _YOYO_HTTPD_BUILTIN
  {
    char *foo = Dicto_Get(rqst->qhdr,"RANGE",0);
    
    if ( foo )
      {
        int start = 0, end = 0;
        if ( sscanf(foo,"bytes=%u-%u",&start,&end) == 2 )
          if ( start >= 0 && end >= start )
            {
              *rng_pos = start;
              *rng_len = end-start+1;
              return 1;
            }
      }
      
    return 0;
  }
#endif
  ;
  
void Httpd_Set_Content_Range(YOYO_HTTPD_REQUEST *rqst, int rng_pos, int rng_len, int total)
#ifdef _YOYO_HTTPD_BUILTIN
  {
    Array_Push(rqst->hlns,
              __Format_Npl("Content-Range: bytes %d-%d/%d",rng_pos,rng_len+rng_pos-1,total));
  }
#endif
  ;
  
char *Httpd_Status_Text(int st)
#ifdef _YOYO_HTTPD_BUILTIN
  {
    switch ( st )
      {
        case 200: return "OK";
        case 201: return "Created";
        case 202: return "Accepted";
        case 203: return "Non-Authoritative Information";
................................................................................
        case 509: return "Bandwidth Limit Exceeded";
        case 510: return "Not Extended";
        case 511: return "Network Authentication Required";
        default:  
          return "Unknown Status"; 
      }
  }
#endif
  ;
  
void Httpd_Gen_Response(YOYO_HTTPD_REQUEST *rqst, int st)
#ifdef _YOYO_HTTPD_BUILTIN
  {
    int i;
    YOYO_BUFFER *bf = rqst->bf;
    bf->count = 0;
    Buffer_Printf(bf,"HTTP/1.1 %d %s\r\n",st,Httpd_Status_Text(st));
    Buffer_Append(bf,"Connection: close\r\n",-1);
    if ( rqst->location )
................................................................................
        else
          Buffer_Printf(bf,"Content-Type: %s\r\n",rqst->outstrm_type);
        if ( rqst->outstrm_length )
          Buffer_Printf(bf,"Content-Length: %d\r\n",rqst->outstrm_length);
        else
          Buffer_Append(bf,"Transfer-Encoding: chunked\r\n",-1);
      }
      
    for ( i = 0; i < rqst->hlns->count; ++i )
      {
        Buffer_Append(bf,rqst->hlns->at[i],-1);
        Buffer_Append(bf,"\r\n",2);
      }
    
    Buffer_Append(bf,"\r\n",2);
    
    if ( rqst->outstrm )
      {
        rqst->left = rqst->outstrm_length;
        if ( rqst->left <= bf->capacity-bf->count)
          {
................................................................................
            int L = Yo_Minu(rqst->left,bf->capacity-bf->count);
            L = Oj_Read(rqst->outstrm,bf->at+bf->count,L,0);
            rqst->left -= L;
            bf->count += L;
          }
      }
  }  
#endif
  ;
  
int Cbk_Httpd_Sending_Result(void *_rqst, int status)
#ifdef _YOYO_HTTPD_BUILTIN
  {
    int L;
    YOYO_HTTPD_REQUEST *rqst = _rqst;
    YOYO_BUFFER *bf = rqst->bf;

    if ( status == ASIO_TIMER )
      return rqst->httpd->callback(rqst->httpd->obj,rqst,HTTPD_STAT_TIMER),0;
................................................................................
        }
      
  finished:
    rqst->httpd->callback(rqst->httpd->obj,rqst,HTTPD_STAT_FINISHED);
    Tcp_Graceful_Close(rqst->sok);
    return 0;
  }
#endif
  ;
  
void Httpd_Continue_Request(YOYO_HTTPD_REQUEST *rqst, int st)
#ifdef _YOYO_HTTPD_BUILTIN
  {
    YOYO_BUFFER *bf = rqst->bf;
    Httpd_Gen_Response(rqst,st);
    Tcp_Asio_Send(rqst->sok,bf->at,bf->count,rqst,Cbk_Httpd_Sending_Result);
  }
#endif
  ;
  
void Httpd_Perform_Request(YOYO_HTTPD_REQUEST *rqst)
#ifdef _YOYO_HTTPD_BUILTIN
  {
    YOYO_BUFFER *bf = rqst->bf;
    int st;
    
    if ( rqst->method == HTTPD_POST_METHOD || rqst->method == HTTPD_PUT_METHOD )
      {
        if ( !rqst->instrm ) 
................................................................................
        Oj_Seek(rqst->instrm,0,0);
      }
      
    st = rqst->httpd->callback(rqst->httpd->obj,rqst,HTTPD_RQST_PERFORM);
    if ( st )
      Httpd_Continue_Request(rqst,st);
  }
#endif
  ;
    
int Cbk_Httpd_Getting_Postdta(void *_rqst, int status, int count)
#ifdef _YOYO_HTTPD_BUILTIN
  {
    YOYO_HTTPD_REQUEST *rqst = _rqst;
    YOYO_BUFFER *bf = rqst->bf;
    
    if ( status == ASIO_TIMER )
      return rqst->httpd->callback(rqst->httpd->obj,rqst,HTTPD_STAT_TIMER),0;
    if ( !ASIO_ST_SUCCEEDED(status) )
................................................................................
    else
      {
        Httpd_Perform_Request(rqst);
      }  
    
    return 0;
  }
#endif
  ;
  
void Httpd_Analyze_Qline(char *text,YOYO_HTTPD_REQUEST *rqst)
#ifdef _YOYO_HTTPD_BUILTIN
  {
    char *S, *q;
    
    S = text;
    while ( *S && *S != '\n' && Isspace(*S) ) ++S;
    q = S;
    while ( *q && !Isspace(*q) ) ++q;
................................................................................
    while ( *S && Isspace(*S) ) ++S;
    q = S;
    while ( *q && !Isspace(*q) ) ++q;
    
    if ( !strncmp_I(S,"HTTP/1.",7) )
      rqst->httpver = strtol(S+7,0,10);
  }
#endif
  ;
  
void Httpd_Analyze_Headers(char *text,YOYO_DICTO *dct)
#ifdef _YOYO_HTTPD_BUILTIN
  {
    int i;
    char *q, *Q, *S = text;
    while (*S!='\n') ++S; /* skip HTTP/1.x query line */
    q = ++S;
................................................................................
        S = ++q;
      }
  }
#endif
  ;

int Cbk_Httpd_Getting_Request(void *_rqst, int status, int count)
#ifdef _YOYO_HTTPD_BUILTIN
  {
    int i;
    YOYO_HTTPD_REQUEST *rqst = _rqst;
    YOYO_BUFFER *bf = rqst->bf;

    if ( status == ASIO_TIMER )
      return rqst->httpd->callback(rqst->httpd->obj,rqst,HTTPD_STAT_TIMER),0;
................................................................................
                     Tcp_Close(rqst->sok),0;

          }
      }

    return 0;
  }
#endif
  ;
  
int Cbk_Httpd_Accept(void *_httpd,int status,YOYO_TCPSOK *sok)
#ifdef _YOYO_HTTPD_BUILTIN
  {
    YOYO_HTTPD *httpd = _httpd;

    if ( status == ASIO_TIMER )
      return httpd->callback(httpd->obj,0,HTTPD_STAT_TIMER),0;

    if ( !ASIO_ST_SUCCEEDED(status) )
................................................................................
          Tcp_Asio_Recv(rqst->sok,rqst->bf->at,rqst->bf->capacity,1,rqst,Cbk_Httpd_Getting_Request);
        else
          httpd->callback(httpd->obj,rqst,HTTPD_STAT_REJECTED);
      }

    return 0;
  }
#endif
  ;
  
void Httpd_Listen(YOYO_HTTPD *httpd, char *host, int port)
#ifdef _YOYO_HTTPD_BUILTIN
  {
    httpd->sok = __Refe(Tcp_Listen(TCPSOK_ASYNC,host,port,0));
    free(httpd->host);
    httpd->host = Str_Copy_Npl(host,-1);
    httpd->port = port;
    Tcp_Asio_Accept(httpd->sok,httpd,Cbk_Httpd_Accept);
  }
#endif
  ;
  
void Http_Shutdown(YOYO_HTTPD *httpd)
#ifdef _YOYO_HTTPD_BUILTIN
  {
    Tcp_Asio_Shutdown(httpd->sok);
  }
#endif
  ;
  
void YOYO_HTTPD_Destruct(YOYO_HTTPD *httpd)
#ifdef _YOYO_HTTPD_BUILTIN
  {
    STRICT_REQUIRE(httpd->rqst == 0);
    free(httpd->host);
    __Unrefe(httpd->obj);
    __Unrefe(httpd->sok);
    __Destruct(httpd);
  }
#endif
  ;
  
YOYO_HTTPD *Httpd_Server(void *obj, httpd_callback_t callback)
#ifdef _YOYO_HTTPD_BUILTIN
  {
    YOYO_HTTPD *httpd = __Object_Dtor(sizeof(YOYO_HTTPD),YOYO_HTTPD_Destruct);
    httpd->obj  = __Refe(obj);
    httpd->callback = callback;
    return httpd;
  }
#endif
  ;
  
#endif /* C_once_BE85262D_CDA8_4AF5_B684_8A30EB6D7370 */

Changes to mime.hc.

72
73
74
75
76
77
78

79
80
81
82
83
84
85
..
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
...
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
...
194
195
196
197
198
199
200

201
202
203
204
205
206
207
...
229
230
231
232
233
234
235

236
237
238
239
240
241
242
243
    YOYO_MIME_GZIP  = __FOUR_CHARS('G','Z','I','P'),
    YOYO_MIME_PKZIP = __FOUR_CHARS('x','Z','I','P'),
    YOYO_MIME_7ZIP  = __FOUR_CHARS('7','Z','I','P'),
    YOYO_MIME_APP   = __FOUR_CHARS('x','A','P','P'),
    YOYO_MIME_JS    = __FOUR_CHARS('x','x','J','S'),
    YOYO_MIME_CSS   = __FOUR_CHARS('x','C','S','S'),
    YOYO_MIME_EXE   = __FOUR_CHARS('x','E','X','E'),

    YOYO_MIME_UNKNOWN = -1,
  };

typedef struct _YOYO_EXT_MAP_RECORD
  {
    char *ext;
    int   extlen;
................................................................................
    {".gif",  4, YOYO_MIME_GIF},
    {".zip",  4, YOYO_MIME_PKZIP},
    {".7z",   3, YOYO_MIME_7ZIP},
    {".gz",   3, YOYO_MIME_GZIP},
    {".exe",  4, YOYO_MIME_APP},
    {".js",   3, YOYO_MIME_JS},
    {".css",  4, YOYO_MIME_CSS},
    
    {0,0}
  }
#endif
  ;

int Mime_Code_Of_Path(char *path, int dflt)
#ifdef _YOYO_MIME_BUILTIN
................................................................................
#endif
  ;
  
int Mime_Code_Of(char *mime_type)
#ifdef _YOYO_MIME_BUILTIN
  {
    int i;
    char S[80] = {0};

    for ( i = 0; mime_type[i] && i < sizeof(S)-1; ++i ) S[i] = Tolower(mime_type[i]);
    if ( !strncmp(S,"application/",12) )
      {


        if ( !strcmp(S+12,"octet-stream") ) return YOYO_MIME_OCTS;
        if ( !strcmp(S+12,"x-javascript") ) return YOYO_MIME_JS;

        if ( !strcmp(S+12,"x-msdownload") ) return YOYO_MIME_EXE;




        return YOYO_MIME_APP;
      }
    else if ( !strncmp(S,"text/",5) )
      {


        if ( !strcmp(S+7,"html") ) return YOYO_MIME_HTML;
        if ( !strcmp(S+7,"css") ) return YOYO_MIME_CSS;
        if ( !strcmp(S+7,"javascript") ) return YOYO_MIME_JS;

        else return YOYO_MIME_TEXT;
      }
    else if ( !strncmp(S,"image/",6) )
      {


        if ( !strcmp(S+6,"jpeg") ) return YOYO_MIME_JPEG;
        if ( !strcmp(S+6,"png") )  return YOYO_MIME_PNG;
        if ( !strcmp(S+6,"gif") )  return YOYO_MIME_GIF;
        return YOYO_MIME_PICT;
      }
    return YOYO_MIME_NONE;
  }
#endif
  ;

................................................................................
  {
    switch(mime)
      {
        case YOYO_MIME_HTML:
        case YOYO_MIME_TEXT:
        case YOYO_MIME_JS:
        case YOYO_MIME_CSS:

          return 1;
      }
    return 0;
  }
#endif
  ;
  
................................................................................
        case YOYO_MIME_GZIP: return "application/x-gzip";
        case YOYO_MIME_PKZIP:return "application/zip";
        case YOYO_MIME_7ZIP: return "application/x-7zip";
        case YOYO_MIME_APP:  return "application/octet-stream";
        case YOYO_MIME_CSS:  return "text/css";
        case YOYO_MIME_JS:   return "text/javascript";
        case YOYO_MIME_EXE:  return "application/x-msdownload";

      }
    return "application/octet-stream";
  }
#endif
  ;
  
#endif /* C_once_E5DC0CBD_6EF6_4E8C_A4F5_CF78FA971011 */








>







 







|







 







|
>
|


>
>
|
<
>
|
>
>
>
>




>
>
|
|
|
>




>
>
|
|
|







 







>







 







>








72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
..
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
...
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
...
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
...
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
    YOYO_MIME_GZIP  = __FOUR_CHARS('G','Z','I','P'),
    YOYO_MIME_PKZIP = __FOUR_CHARS('x','Z','I','P'),
    YOYO_MIME_7ZIP  = __FOUR_CHARS('7','Z','I','P'),
    YOYO_MIME_APP   = __FOUR_CHARS('x','A','P','P'),
    YOYO_MIME_JS    = __FOUR_CHARS('x','x','J','S'),
    YOYO_MIME_CSS   = __FOUR_CHARS('x','C','S','S'),
    YOYO_MIME_EXE   = __FOUR_CHARS('x','E','X','E'),
    YOYO_MIME_JSON  = __FOUR_CHARS('J','S','O','N'),
    YOYO_MIME_UNKNOWN = -1,
  };

typedef struct _YOYO_EXT_MAP_RECORD
  {
    char *ext;
    int   extlen;
................................................................................
    {".gif",  4, YOYO_MIME_GIF},
    {".zip",  4, YOYO_MIME_PKZIP},
    {".7z",   3, YOYO_MIME_7ZIP},
    {".gz",   3, YOYO_MIME_GZIP},
    {".exe",  4, YOYO_MIME_APP},
    {".js",   3, YOYO_MIME_JS},
    {".css",  4, YOYO_MIME_CSS},
    {".json", 5, YOYO_MIME_JSON},
    {0,0}
  }
#endif
  ;

int Mime_Code_Of_Path(char *path, int dflt)
#ifdef _YOYO_MIME_BUILTIN
................................................................................
#endif
  ;
  
int Mime_Code_Of(char *mime_type)
#ifdef _YOYO_MIME_BUILTIN
  {
    int i;
    char _S[80] = {0};
    char *S = _S;
    for ( i = 0; mime_type[i] && i < sizeof(_S)-1; ++i ) _S[i] = Tolower(mime_type[i]);
    if ( !strncmp(S,"application/",12) )
      {
        S += 12;
        if ( !strncmp(S,"x-",2) ) S +=2;
        if ( !strcmp(S,"octet-stream") ) return YOYO_MIME_OCTS;

        if ( !strcmp(S,"javascript") )   return YOYO_MIME_JS;
        if ( !strcmp(S,"msdownload") )   return YOYO_MIME_EXE;
        if ( !strcmp(S,"json") )         return YOYO_MIME_JSON;
        if ( !strcmp(S,"gzip") )         return YOYO_MIME_GZIP;
        if ( !strcmp(S,"zip") )          return YOYO_MIME_PKZIP;
        if ( !strcmp(S,"7zip") )         return YOYO_MIME_7ZIP;
        return YOYO_MIME_APP;
      }
    else if ( !strncmp(S,"text/",5) )
      {
        S += 5;
        if ( !strncmp(S,"x-",2) ) S +=2;
        if ( !strcmp(S,"html") ) return YOYO_MIME_HTML;
        if ( !strcmp(S,"css") )   return YOYO_MIME_CSS;
        if ( !strcmp(S,"javascript") ) return YOYO_MIME_JS;
        if ( !strcmp(S,"json") ) return YOYO_MIME_JSON;
        else return YOYO_MIME_TEXT;
      }
    else if ( !strncmp(S,"image/",6) )
      {
        S += 6;
        if ( !strncmp(S,"x-",2) ) S +=2;
        if ( !strcmp(S,"jpeg") ) return YOYO_MIME_JPEG;
        if ( !strcmp(S,"png") )  return YOYO_MIME_PNG;
        if ( !strcmp(S,"gif") )  return YOYO_MIME_GIF;
        return YOYO_MIME_PICT;
      }
    return YOYO_MIME_NONE;
  }
#endif
  ;

................................................................................
  {
    switch(mime)
      {
        case YOYO_MIME_HTML:
        case YOYO_MIME_TEXT:
        case YOYO_MIME_JS:
        case YOYO_MIME_CSS:
        case YOYO_MIME_JSON:
          return 1;
      }
    return 0;
  }
#endif
  ;
  
................................................................................
        case YOYO_MIME_GZIP: return "application/x-gzip";
        case YOYO_MIME_PKZIP:return "application/zip";
        case YOYO_MIME_7ZIP: return "application/x-7zip";
        case YOYO_MIME_APP:  return "application/octet-stream";
        case YOYO_MIME_CSS:  return "text/css";
        case YOYO_MIME_JS:   return "text/javascript";
        case YOYO_MIME_EXE:  return "application/x-msdownload";
        case YOYO_MIME_JSON: return "application/json";
      }
    return "application/octet-stream";
  }
#endif
  ;
  
#endif /* C_once_E5DC0CBD_6EF6_4E8C_A4F5_CF78FA971011 */

Changes to string.hc.

1702
1703
1704
1705
1706
1707
1708

1709
1710

1711
1712
1713
1714
1715
1716
1717
1718
    
    __Elm_Append_Npl(&R,R_count,"\0",1,1,&R_capacity);
    return R;
  }
#endif
  ;
  

#define Str_Utf8_Upper(S,L) ((char*)__Pool(Str_Utf8_Upper_Npl(S,L)))
#define Str_Utf8_Upper_Npl(S,L) Str_Utf8_Transform_Npl(S,L,(void*)towupper)

#define Str_Utf8_Lower(S,L) ((char*)__Pool(Str_Utf8_Lower_Npl(S,L)))
#define Str_Utf8_Lower_Npl(S,L) Str_Utf8_Transform_Npl(S,L,(void*)towlower)

char *Str_Safe_Quote(char *S)
#ifdef _YOYO_STRING_BUILTIN  
  {
    int S_len = S? strlen(S):0;
    int R_count = 0;







>
|

>
|







1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
    
    __Elm_Append_Npl(&R,R_count,"\0",1,1,&R_capacity);
    return R;
  }
#endif
  ;
  
#define Str_Utf8_Upper(S)   Str_Utf8_Upper_L(S,-1)
#define Str_Utf8_Upper_L(S,L) ((char*)__Pool(Str_Utf8_Upper_Npl(S,L)))
#define Str_Utf8_Upper_Npl(S,L) Str_Utf8_Transform_Npl(S,L,(void*)towupper)
#define Str_Utf8_Lower(S)     Str_Utf8_Lower_L(S,-1)
#define Str_Utf8_Lower_L(S,L) ((char*)__Pool(Str_Utf8_Lower_Npl(S,L)))
#define Str_Utf8_Lower_Npl(S,L) Str_Utf8_Transform_Npl(S,L,(void*)towlower)

char *Str_Safe_Quote(char *S)
#ifdef _YOYO_STRING_BUILTIN  
  {
    int S_len = S? strlen(S):0;
    int R_count = 0;

Changes to tcpip.hc.

155
156
157
158
159
160
161


162
163
164
165
166
167
168

ipaddr_t Dns_Resolve(char *host)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    ipaddr_t ip;
    switch ( Dns_IPv4_Resolve(host,&ip) )
      {


        case NO_RECOVERY:
          __Raise(YOYO_ERROR_DNS,"unrecoverable DNS error");
        default: 
          __Raise_Format(YOYO_ERROR_DNS,("DNS couldn't resolve ip for name %s",host));
      }
    return ip;
  }        







>
>







155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170

ipaddr_t Dns_Resolve(char *host)
#ifdef _YOYO_TCPIP_BUILTIN
  {
    ipaddr_t ip;
    switch ( Dns_IPv4_Resolve(host,&ip) )
      {
        case 0:
          break;
        case NO_RECOVERY:
          __Raise(YOYO_ERROR_DNS,"unrecoverable DNS error");
        default: 
          __Raise_Format(YOYO_ERROR_DNS,("DNS couldn't resolve ip for name %s",host));
      }
    return ip;
  }        

Changes to uname.hc.

195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
    char *ret = 0;
    YOYO_CPUINFO _cpui;
    
    if ( !cpui )
      {
        cpui = &_cpui;
        if ( !Get_Cpu_Info(cpui) )
          return Str_Copy("unknown",-1);
      }
    
    __Auto_Ptr(ret)
      {
        int i = 0;
        char *Q[32] = {0};
        







|







195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
    char *ret = 0;
    YOYO_CPUINFO _cpui;
    
    if ( !cpui )
      {
        cpui = &_cpui;
        if ( !Get_Cpu_Info(cpui) )
          return Str_Copy("unknown");
      }
    
    __Auto_Ptr(ret)
      {
        int i = 0;
        char *Q[32] = {0};
        

Changes to url.hc.

228
229
230
231
232
233
234


235











236
237
238
239
240
241
242
              }
          }
          
        urlout->endpoint = __Retain(endpoint);
        
        if ( flags & URLX_PARSE_ARGUMENTS )
          {


            urlout->argv = __Refe(Dicto_Ptrs());











          }
      }
      
    return urlout;
  }
#endif
  ;







>
>

>
>
>
>
>
>
>
>
>
>
>







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
              }
          }
          
        urlout->endpoint = __Retain(endpoint);
        
        if ( flags & URLX_PARSE_ARGUMENTS )
          {
            int i;
            YOYO_ARRAY *a = Str_Split(urlout->args,"&");
            urlout->argv = __Refe(Dicto_Ptrs());
            for ( i = 0; i < a->count; ++i )
              {
                char *v = 0;
                char *p = strchr(a->at[i],'=');
                if ( p )
                  {
                    v = p+1;
                    *p = 0;
                  }
                Dicto_Put(urlout->argv,a->at[i],Str_Copy_Npl(v,-1));
              }
          }
      }
      
    return urlout;
  }
#endif
  ;

Changes to webget.hc.

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
..
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
...
107
108
109
110
111
112
113

114

115

116
117
118
119
120
121
122
123
124
125
126
127
128
...
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
...
197
198
199
200
201
202
203

204



































205

206
207
208
209
210
211
212
...
235
236
237
238
239
240
241

242

243

244
245
246
247
248
249
250
...
252
253
254
255
256
257
258

259

260

261
262
263
264
265
266
267
268
269

270

271
272
273

274
275
276
277
278
279
280
281
282
283
284

285

286

287
288
289
290
291
292
293
294
295
296

297










































298
299
300

301
302

303
304
305
306
307
308
309
...
334
335
336
337
338
339
340
341
342
343
344
345
346


347
348
349
350
351
352
353
354
355
356





357
358
359
360
361
362
363
364

365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395

396
397
398

399

400

401
402
403
404
405
406
407
408
409
410
411

412














413

414
415
416
417
418
419
420
...
443
444
445
446
447
448
449

450

451

452
453
454
455
456
457
458
459
460
461

462

463

464
465
466
467
468
469
470
...
476
477
478
479
480
481
482

483

484
485

486
487
488
489
490
491
492
...
525
526
527
528
529
530
531

532
533
534
535

536

537

538
539
540
541

542

543

544
545
546
547
548
549
550
...
553
554
555
556
557
558
559

560

561
562
563
564

565
566
567
568
569
570
571
572
573
574

575

576

577
578


579










580
581
582

583

584

585
586

587

588

589
590
591

592

593

594
595
596
597
598
599
600
601
602

603

604

605
606
607
608
609
610
611
612
613


614
615
616
#include "httpx.hc"

#ifdef _LIBYOYO
#define _YOYO_WEBGET_BUILTIN
#endif

enum { 
  WEBGET_FILE_BLOCK_SIZE = 64*KILOBYTE, 
  //WEBGET_BLOCKS_PER_WRK  = 8, 
  WEBGET_WORKERS_COUNT   = 3,
  WEBGET_SIGNATURE       = __FOUR_CHARS('W','G','E','T'),
  WEBGET_FINISHED        = 200,
  WEBGET_PARTIAL         = 206,
  WEBGET_FAILED          = 404,       
  WEBGET_STARTED         = 100,       
................................................................................
    int workers_max;
    webget_callback_t callback;
    void *cbkobj;
    char *output;
    void *outstrm;
    YOYO_WEBGET_PRIOR *prior;
    char *fail_reason;

    int aborting: 1;

  } YOYO_WEBGET;

typedef struct _YOYO_WEBGET_WKR
  {
    YOYO_WEBGET *webget;
    int offs, len;
    YOYO_BUFFER *bf;
  } YOYO_WEBGET_WRK;

void YOYO_WEBGET_PRIOR_Destruct(YOYO_WEBGET_PRIOR *prior)

  {
    __Unrefe(prior->cbkobj);
    __Unrefe(prior->next);
    __Destruct(prior);
  }

 

void YOYO_WEBGET_WRK_Destruct(YOYO_WEBGET_WRK *wrk)

  {
    __Unrefe(wrk->bf);
    __Unrefe(wrk->webget);
    __Destruct(wrk);
  }



int Webget_Range_Ready(YOYO_WEBGET *webget, int rng_offs, int rng_len)

  {
    int i,j;
    YOYO_WEBGET_STATE *st = webget->state;

    if ( rng_len )
      {
        j = (Yo_Mini(rng_offs+rng_len,st->h.file_length)+st->h.block_size-1)/st->h.block_size;
................................................................................
                return 0;
              }
          }
      }
  
    return 1;
  }



int _Webget_St_Next(YOYO_WEBGET_STATE *st, int *len, int rng_offs, int rng_len)

  {
    int i,j,n;

    if ( rng_len )
      {
        j = (Yo_Mini(rng_len,st->h.file_length)+st->h.block_size-1)/st->h.block_size;
        i = rng_offs/st->h.block_size;
        for ( ; i < j; ++i )
          {
            if ( !(st->h.map[i/8] & (1<<i%8)) )
              {
                int offs = i*st->h.block_size;
                *len = Yo_Mini(st->h.block_size,st->h.file_length-offs);
................................................................................
                  }
              }
        }
   exit:
    *len = 0;
    return 0;
  }



void _Webget_St_Ready(YOYO_WEBGET_STATE *st,int offs)

  {
    int bno = offs/st->h.block_size;
    int len = Yo_Mini(st->h.block_size,st->h.file_length-offs);
    STRICT_REQUIRE(offs%st->h.block_size == 0);
    STRICT_REQUIRE(offs<st->h.file_length);
    STRICT_REQUIRE(bno/8<st->h.map_length);
    STRICT_REQUIRE(st->h.map[bno/8]&(1<<(bno%8)) != 0);
    STRICT_REQUIRE(st->map[bno/8]&(1<<(bno%8)) == 0);
    st->map[bno/8] |= (1 << (bno%8));
    st->h.ready += len;
  }



int _Webget_St_Is_Ready(YOYO_WEBGET_STATE *st,int offs)

  {
    int bno = offs/st->h.block_size;
    STRICT_REQUIRE(offs%st->h.block_size == 0);
    STRICT_REQUIRE(offs<st->h.file_length);
    STRICT_REQUIRE(bno/8<st->h.map_length);
    return !! (st->map[bno/8] & (1 << (bno%8)));
  }



int _Webget_St_In_Process(YOYO_WEBGET_STATE *st,int offs)

  {
    int bno = offs/st->h.block_size;
    STRICT_REQUIRE(offs%st->h.block_size == 0);
    STRICT_REQUIRE(offs<st->h.file_length);
    STRICT_REQUIRE(bno/8<st->h.map_length);
    return !! (st->h.map[bno/8] & (1 << (bno%8)));
  }



void _Webget_Init_Statemap(YOYO_WEBGET *webget, int length, byte_t *md5)

  {
    YOYO_WEBGET_STATE *st=0;
    int map_length = 0;
    
    STRICT_REQUIRE(!webget->state);
    
    map_length = (length+WEBGET_FILE_BLOCK_SIZE-1)/WEBGET_FILE_BLOCK_SIZE;
................................................................................
    st = __Zero_Malloc_Npl(sizeof(st->h)+map_length+20);
    st->h.block_size = WEBGET_FILE_BLOCK_SIZE;
    st->h.file_length = length;
    st->h.map_length = map_length;
    st->h.map = __Zero_Malloc_Npl(map_length);
    webget->state = st;
  }

  



































void _Webget_Load_Statemap(YOYO_WEBGET *webget, int length, byte_t *md5)

  {
    __Try_Except
      if ( webget->statemap ) __Auto_Release
        {
          STRICT_REQUIRE( !webget->state );
          if ( File_Exists(webget->statemap) )
            {
................................................................................
            }
        }
    __Except
      ;
    if ( !webget->state )
      _Webget_Init_Statemap(webget,length,md5);
  }



void _Webget_Write_Statemap(YOYO_WEBGET *webget)

  {
    __Try_Except
      if ( webget->statemap ) __Auto_Release
        {
          byte_t b20[20];
          int state_L = sizeof(webget->state->h)+webget->state->h.map_length;
          void *f = Cfile_Open(webget->statemap,"Pw+");
................................................................................
          Oj_Write_Full(f,webget->state,state_L);
          Sha1_Digest(webget->state,state_L,b20);
          Oj_Write_Full(f,b20,20);
        }
    __Except
      ;
  }

  

void _Webget_Prior_Deque(YOYO_WEBGET *webget)

  {
    STRICT_REQUIRE(webget->prior!=0);
    if ( webget->prior ) __Auto_Release
      {
        YOYO_WEBGET_PRIOR *prior = __Pool_RefPtr(webget->prior);
        webget->prior = prior->next;
        prior->next = 0;
      }
  }



#define _Webget_Prior_Finished(Webget) _Webget_Prior_Exit(Webget,WEBGET_FINISHED);
#define _Webget_Prior_Failed(Webget) _Webget_Prior_Exit(Webget,WEBGET_FAILED);
void _Webget_Prior_Exit(YOYO_WEBGET *webget,int status)

  {
    STRICT_REQUIRE(webget->prior!=0);
    if ( webget->prior ) __Auto_Release
      {
        YOYO_WEBGET_PRIOR *prior = __Pool_RefPtr(webget->prior);
        webget->prior = prior->next;
        prior->next = 0;
        if ( prior->callback ) 
          prior->callback(prior->cbkobj,status,prior->offs,prior->len,webget);
      }
  }



void Webget_Priority_Part(YOYO_WEBGET *webget, int pos, int len, prior_callback_t callback, void *obj)

  {
    YOYO_WEBGET_PRIOR *prior = __Object_Dtor(sizeof(YOYO_WEBGET_PRIOR),YOYO_WEBGET_PRIOR_Destruct);
    YOYO_WEBGET_PRIOR **last = &webget->prior;
    prior->offs = pos;
    prior->len  = len;
    prior->callback = callback;
    prior->cbkobj = __Refe(obj);
    while ( *last ) last = &(*last)->next;
    *last = __Refe(prior);
  }












































#define Webget_Wrk_Query_Next(Wrk,Httpx) Webget_Wrk_Query(Wrk,Httpx,0)
#define Webget_Wrk_Query_Retry(Wrk,Httpx) Webget_Wrk_Query(Wrk,Httpx,1)


void Cbk_Webget_Get_Callback(void *obj, YOYO_HTTPX *httpx, int status);
void Webget_Wrk_Query(YOYO_WEBGET_WRK *wrk, YOYO_HTTPX *httpx, int retry)

  {
    YOYO_WEBGET *webget = wrk->webget;
    YOYO_WEBGET_STATE *st = webget->state;
    
    __Gogo
      {
        YOYO_WEBGET_PRIOR **prior = &webget->prior;
................................................................................
        while ( prior )
          {
            wrk->offs = _Webget_St_Next(st,&wrk->len,prior->offs,prior->len);
            if ( wrk->len )
              break;
            prior = prior->next;
          }
        if ( !wrk->len )
          wrk->offs = _Webget_St_Next(st,&wrk->len,0,0);
      }
    
    if ( !wrk->len ) __Auto_Release
      {


        __Pool_RefPtr(webget);
        wrk->webget = 0;
        --webget->workers_cnt;
        STRICT_REQUIRE( webget->workers_cnt >= 0 );
        
        if ( !webget->workers_cnt )
          {
            if ( webget->callback )
              webget->callback(webget->cbkobj,WEBGET_FINISHED,0,webget);
          }





      }
    else
      { 
        YOYO_DICTO *hdrs = Httpx_Hdrs_Set_Range(0,wrk->offs,wrk->len);
        wrk->bf->count = 0;
        Httpx_Query(httpx,HTTPX_GET,0,hdrs,0,Buffer_As_File(wrk->bf),st->h.block_size);
      }
  }


int Webget_Ready(YOYO_WEBGET *webget)
  {
    if ( webget->state )
      return webget->state->h.ready;
    return 0;
  }

int Webget_Done(YOYO_WEBGET *webget)
  {
    if ( webget->state )
      return webget->state->h.ready == webget->state->h.file_length;
    return 0;
  }

int Webget_Length(YOYO_WEBGET *webget)
  {
    if ( webget->state )
      return webget->state->h.file_length;
    return 0;
  }

int Webget_Block_Length(YOYO_WEBGET *webget, int offs)
  {
    YOYO_WEBGET_STATE *st = webget->state;
    if ( st && offs < st->h.file_length )
      return Yo_Mini(st->h.block_size,st->h.file_length-offs);
    return 0;
  }
  
void _Terminate_All_Wrks(YOYO_WEBGET *webget)

  {
    //
  }



void _Webget_Abort(YOYO_WEBGET *webget, char *reason)

  {
    __Enter_Once(webget->aborting)
      {
        webget->fail_reason = Str_Copy_Npl(reason,-1);
        _Terminate_All_Wrks(webget);
        while ( webget->prior )
          _Webget_Prior_Failed(webget);
        if ( webget->callback )
          webget->callback(webget->cbkobj,WEBGET_FAILED,0,webget);
      }
  }
















void Cbk_Webget_Get_Callback(void *obj, YOYO_HTTPX *httpx, int status)

  {
    YOYO_WEBGET_WRK *wrk = obj;
    if ( !HTTPX_SUCCEEDED(status) )
      {
        if ( !httpx->interrupted && !(status&HTTPX_PERMANENT_ERROR))
          {
            Webget_Wrk_Query_Retry(wrk,0);
................................................................................
          }
        else
          {
            _Webget_Abort(wrk->webget,httpx->status_text);
          }
      }
  }



void Webget_Start_Worker(YOYO_WEBGET *webget)

  {
    if ( !Webget_Done(webget) )
      {
        YOYO_WEBGET_WRK *wrk = __Object_Dtor(sizeof(YOYO_WEBGET_WRK),YOYO_WEBGET_WRK_Destruct);
        wrk->webget = __Refe(webget);
        wrk->bf = __Refe(Buffer_Reserve(0,webget->state?webget->state->h.block_size:0));
        ++webget->workers_cnt;
        Webget_Wrk_Query_Next(wrk,0);
      }
  }



void _Webget_Start_Workers(YOYO_WEBGET *webget)

  {
    int i;
    if ( Webget_Done(webget) )
      {
        while ( webget->prior ) __Auto_Release
          {
            YOYO_WEBGET_PRIOR *p = __Pool_RefPtr(webget->prior);
................................................................................
        if ( webget->callback )
          webget->callback(webget->cbkobj,WEBGET_FINISHED,0,webget);
      }
    else
      for ( i = 0; i < webget->workers_max; ++i )
        Webget_Start_Worker(webget);
  }



void _Webget_Query_Head(YOYO_WEBGET *webget);
void Cbk_Webget_Head_Callback(void *obj, YOYO_HTTPX *httpx, int status)

  {
    YOYO_WEBGET *webget = obj;
    if ( !HTTPX_SUCCEEDED(status) )
      {
        if ( !httpx->interrupted && !(status&HTTPX_PERMANENT_ERROR) )
          {
            _Webget_Query_Head(webget);
................................................................................
                if ( webget->callback )
                  webget->callback(webget->cbkobj,WEBGET_BAD_OUTPUT,0,webget);
                _Webget_Abort(webget,__yoTa("bad output",0));
              }
          }
        else
          {

            _Webget_Abort(webget,httpx->status_text);
          }
      }
  }



void _Webget_Query_Head(YOYO_WEBGET *webget)

  {
    YOYO_HTTPX *httpx = Httpx_Client_Callback(webget->source,HTTPX_ASYNC,Cbk_Webget_Head_Callback,webget);
    Httpx_Query(httpx,HTTPX_HEAD,0,0,0,0,0);
  }



void YOYO_WEBGET_Destruct(YOYO_WEBGET *webget)

  {
    __Unrefe(webget->cbkobj);
    __Unrefe(webget->outstrm);
    if ( webget->state )
      free(webget->state->h.map);
    free(webget->state);
    free(webget->statemap);
................................................................................
    free(webget->output);
    
    while ( webget->prior )
      _Webget_Prior_Failed(webget);
      
    __Destruct(webget);
  }



YOYO_WEBGET *Webget_Init(
  char *url, char *output, char *statemap, 
  webget_callback_t callback, void *obj,
  int workers)

  {
    YOYO_WEBGET *webget = __Object_Dtor(sizeof(YOYO_WEBGET),YOYO_WEBGET_Destruct);
    webget->statemap = statemap?Str_Copy_Npl(statemap,-1):0;
    webget->output   = output?Str_Copy_Npl(output,-1):0;
    webget->callback = callback;
    webget->cbkobj   = __Refe(obj);
    webget->workers_max = workers>0?workers:WEBGET_WORKERS_COUNT;
    webget->source   = Str_Copy_Npl(url,-1);
    return webget;
  }



void Webget_Start(YOYO_WEBGET *webget)

  {
    if ( !webget->state )


      _Webget_Query_Head(webget);










    else if ( !webget->workers_cnt )
      _Webget_Start_Workers(webget);
  }

  

void Webget_Stop(YOYO_WEBGET *webget)

  {
  }



void Webget_Abort(YOYO_WEBGET *webget)

  {
    _Webget_Abort(webget,__yoTa("manual abort",0));
  }

  

YOYO_WEBGET *Webget_Download(char *url, char *output, webget_callback_t callback, void *obj)

  {
    YOYO_WEBGET *webget = 0;
    __Auto_Ptr(webget)
      {
        webget = Webget_Init(url,output,0,callback,obj,WEBGET_WORKERS_COUNT);
        Webget_Start(webget);
      }
    return webget;
  }



YOYO_WEBGET *Webget_Reget(char *url, char *output, char *statemap, webget_callback_t callback, void *obj)

  {
    YOYO_WEBGET *webget = 0;
    __Auto_Ptr(webget)
      {
        webget = Webget_Init(url,output,statemap,callback,obj,WEBGET_WORKERS_COUNT);
        Webget_Start(webget);
      }
    return webget;
  }



#endif /*C_once_A680A0EB_B8EE_4F96_B06B_74BA1C9C8136*/








|







 







>

>










>





>
|
>

>





>
|
>

>







 







>
|
>

>





|







 







>
|
>

>











>
|
>

>







>
|
>

>







>
|
>

>







 







>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

>







 







>
|
>

>







 







>
|
>

>









>
|
>



>











>
|
>

>










>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



>


>







 







|





>
>
|
|
|
|
|
|
|
|
|
|
>
>
>
>
>








>
|
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<

>



>
|
>

>











>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>

>







 







>
|
>

>










>
|
>

>







 







>
|
>


>







 







>




>
|
>

>




>
|
>

>







 







>
|
>




>










>
|
>

>


>
>
|
>
>
>
>
>
>
>
>
>
>



>
|
>

>


>
|
>

>



>
|
>

>









>
|
>

>









>
>



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
..
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
...
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
...
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
...
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
...
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
...
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
...
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491

492



























493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
...
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
...
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
...
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
...
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
#include "httpx.hc"

#ifdef _LIBYOYO
#define _YOYO_WEBGET_BUILTIN
#endif

enum { 
  WEBGET_FILE_BLOCK_SIZE = 128*KILOBYTE, 
  //WEBGET_BLOCKS_PER_WRK  = 8, 
  WEBGET_WORKERS_COUNT   = 3,
  WEBGET_SIGNATURE       = __FOUR_CHARS('W','G','E','T'),
  WEBGET_FINISHED        = 200,
  WEBGET_PARTIAL         = 206,
  WEBGET_FAILED          = 404,       
  WEBGET_STARTED         = 100,       
................................................................................
    int workers_max;
    webget_callback_t callback;
    void *cbkobj;
    char *output;
    void *outstrm;
    YOYO_WEBGET_PRIOR *prior;
    char *fail_reason;
    int  httpx_fail_status;
    int aborting: 1;
    int ondemand: 1;
  } YOYO_WEBGET;

typedef struct _YOYO_WEBGET_WKR
  {
    YOYO_WEBGET *webget;
    int offs, len;
    YOYO_BUFFER *bf;
  } YOYO_WEBGET_WRK;

void YOYO_WEBGET_PRIOR_Destruct(YOYO_WEBGET_PRIOR *prior)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    __Unrefe(prior->cbkobj);
    __Unrefe(prior->next);
    __Destruct(prior);
  }
#endif
  ;
   
void YOYO_WEBGET_WRK_Destruct(YOYO_WEBGET_WRK *wrk)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    __Unrefe(wrk->bf);
    __Unrefe(wrk->webget);
    __Destruct(wrk);
  }
#endif
  ;
  
int Webget_Range_Ready(YOYO_WEBGET *webget, int rng_offs, int rng_len)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    int i,j;
    YOYO_WEBGET_STATE *st = webget->state;

    if ( rng_len )
      {
        j = (Yo_Mini(rng_offs+rng_len,st->h.file_length)+st->h.block_size-1)/st->h.block_size;
................................................................................
                return 0;
              }
          }
      }
  
    return 1;
  }
#endif
  ;
  
int _Webget_St_Next(YOYO_WEBGET_STATE *st, int *len, int rng_offs, int rng_len)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    int i,j,n;

    if ( rng_len )
      {
        j = (Yo_Mini(rng_offs+rng_len,st->h.file_length)+st->h.block_size-1)/st->h.block_size;
        i = rng_offs/st->h.block_size;
        for ( ; i < j; ++i )
          {
            if ( !(st->h.map[i/8] & (1<<i%8)) )
              {
                int offs = i*st->h.block_size;
                *len = Yo_Mini(st->h.block_size,st->h.file_length-offs);
................................................................................
                  }
              }
        }
   exit:
    *len = 0;
    return 0;
  }
#endif
  ;
  
void _Webget_St_Ready(YOYO_WEBGET_STATE *st,int offs)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    int bno = offs/st->h.block_size;
    int len = Yo_Mini(st->h.block_size,st->h.file_length-offs);
    STRICT_REQUIRE(offs%st->h.block_size == 0);
    STRICT_REQUIRE(offs<st->h.file_length);
    STRICT_REQUIRE(bno/8<st->h.map_length);
    STRICT_REQUIRE(st->h.map[bno/8]&(1<<(bno%8)) != 0);
    STRICT_REQUIRE(st->map[bno/8]&(1<<(bno%8)) == 0);
    st->map[bno/8] |= (1 << (bno%8));
    st->h.ready += len;
  }
#endif
  ;
  
int _Webget_St_Is_Ready(YOYO_WEBGET_STATE *st,int offs)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    int bno = offs/st->h.block_size;
    STRICT_REQUIRE(offs%st->h.block_size == 0);
    STRICT_REQUIRE(offs<st->h.file_length);
    STRICT_REQUIRE(bno/8<st->h.map_length);
    return !! (st->map[bno/8] & (1 << (bno%8)));
  }
#endif
  ;
  
int _Webget_St_In_Process(YOYO_WEBGET_STATE *st,int offs)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    int bno = offs/st->h.block_size;
    STRICT_REQUIRE(offs%st->h.block_size == 0);
    STRICT_REQUIRE(offs<st->h.file_length);
    STRICT_REQUIRE(bno/8<st->h.map_length);
    return !! (st->h.map[bno/8] & (1 << (bno%8)));
  }
#endif
  ;
  
void _Webget_Init_Statemap(YOYO_WEBGET *webget, int length, byte_t *md5)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    YOYO_WEBGET_STATE *st=0;
    int map_length = 0;
    
    STRICT_REQUIRE(!webget->state);
    
    map_length = (length+WEBGET_FILE_BLOCK_SIZE-1)/WEBGET_FILE_BLOCK_SIZE;
................................................................................
    st = __Zero_Malloc_Npl(sizeof(st->h)+map_length+20);
    st->h.block_size = WEBGET_FILE_BLOCK_SIZE;
    st->h.file_length = length;
    st->h.map_length = map_length;
    st->h.map = __Zero_Malloc_Npl(map_length);
    webget->state = st;
  }
#endif
  ;
    
int _Webget_Completed_Test_1(YOYO_WEBGET_STATE *st, char *stmap, char *output)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    int completed = 0;
    memset(st,0,sizeof(*st));
    if ( stmap ) __Auto_Release
      if ( File_Exists(stmap) )
        {
          YOYO_WEBGET_STATE st = {0};
          void *f = Cfile_Open(stmap,"r"); 
          Oj_Read_Full(f,&st,sizeof(YOYO_WEBGET_STATE));
          if ( st.h.signature == WEBGET_SIGNATURE
            && st.h.ready == st.h.file_length ) 
            {
              YOYO_FILE_STATS fst = {0};
              if ( File_Get_Stats(output,&fst,1)->f.exists
                && fst.length == st.h.file_length ) 
                completed = 1;
            }
        }
    return completed;
  }
#endif
  ;

int _Webget_Completed_Test(YOYO_WEBGET *webget)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    YOYO_WEBGET_STATE st = {0};
    return _Webget_Completed_Test_1(&st,webget->statemap,webget->output);
  }
#endif
  ;
    
void _Webget_Load_Statemap(YOYO_WEBGET *webget, int length, byte_t *md5)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    __Try_Except
      if ( webget->statemap ) __Auto_Release
        {
          STRICT_REQUIRE( !webget->state );
          if ( File_Exists(webget->statemap) )
            {
................................................................................
            }
        }
    __Except
      ;
    if ( !webget->state )
      _Webget_Init_Statemap(webget,length,md5);
  }
#endif
  ;
  
void _Webget_Write_Statemap(YOYO_WEBGET *webget)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    __Try_Except
      if ( webget->statemap ) __Auto_Release
        {
          byte_t b20[20];
          int state_L = sizeof(webget->state->h)+webget->state->h.map_length;
          void *f = Cfile_Open(webget->statemap,"Pw+");
................................................................................
          Oj_Write_Full(f,webget->state,state_L);
          Sha1_Digest(webget->state,state_L,b20);
          Oj_Write_Full(f,b20,20);
        }
    __Except
      ;
  }
#endif
  ;
    
void _Webget_Prior_Deque(YOYO_WEBGET *webget)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    STRICT_REQUIRE(webget->prior!=0);
    if ( webget->prior ) __Auto_Release
      {
        YOYO_WEBGET_PRIOR *prior = __Pool_RefPtr(webget->prior);
        webget->prior = prior->next;
        prior->next = 0;
      }
  }
#endif
  ;
  
#define _Webget_Prior_Finished(Webget) _Webget_Prior_Exit(Webget,WEBGET_FINISHED);
#define _Webget_Prior_Failed(Webget) _Webget_Prior_Exit(Webget,WEBGET_FAILED);
void _Webget_Prior_Exit(YOYO_WEBGET *webget,int status)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    STRICT_REQUIRE(webget->prior!=0);
    if ( webget->prior ) __Auto_Release
      {
        YOYO_WEBGET_PRIOR *prior = __Pool_RefPtr(webget->prior);
        webget->prior = prior->next;
        prior->next = 0;
        if ( prior->callback ) 
          prior->callback(prior->cbkobj,status,prior->offs,prior->len,webget);
      }
  }
#endif
  ;
  
void Webget_Priority_Part(YOYO_WEBGET *webget, int pos, int len, prior_callback_t callback, void *obj)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    YOYO_WEBGET_PRIOR *prior = __Object_Dtor(sizeof(YOYO_WEBGET_PRIOR),YOYO_WEBGET_PRIOR_Destruct);
    YOYO_WEBGET_PRIOR **last = &webget->prior;
    prior->offs = pos;
    prior->len  = len;
    prior->callback = callback;
    prior->cbkobj = __Refe(obj);
    while ( *last ) last = &(*last)->next;
    *last = __Refe(prior);
  }
#endif
  ;
  
int Webget_Ready(YOYO_WEBGET *webget)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    if ( webget->state )
      return webget->state->h.ready;
    return 0;
  }
#endif
  ;
  
int Webget_Done(YOYO_WEBGET *webget)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    if ( webget->state )
      return webget->state->h.ready == webget->state->h.file_length;
    return 0;
  }
#endif
  ;
  
int Webget_Length(YOYO_WEBGET *webget)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    if ( webget->state )
      return webget->state->h.file_length;
    return 0;
  }
#endif
  ;
  
int Webget_Block_Length(YOYO_WEBGET *webget, int offs)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    YOYO_WEBGET_STATE *st = webget->state;
    if ( st && offs < st->h.file_length )
      return Yo_Mini(st->h.block_size,st->h.file_length-offs);
    return 0;
  }
#endif
  ;
  
#define Webget_Wrk_Query_Next(Wrk,Httpx) Webget_Wrk_Query(Wrk,Httpx,0)
#define Webget_Wrk_Query_Retry(Wrk,Httpx) Webget_Wrk_Query(Wrk,Httpx,1)

void Cbk_Webget_Alert_Callback(YOYO_WEBGET_WRK *wrk, int status);
void Cbk_Webget_Get_Callback(void *obj, YOYO_HTTPX *httpx, int status);
void Webget_Wrk_Query(YOYO_WEBGET_WRK *wrk, YOYO_HTTPX *httpx, int retry)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    YOYO_WEBGET *webget = wrk->webget;
    YOYO_WEBGET_STATE *st = webget->state;
    
    __Gogo
      {
        YOYO_WEBGET_PRIOR **prior = &webget->prior;
................................................................................
        while ( prior )
          {
            wrk->offs = _Webget_St_Next(st,&wrk->len,prior->offs,prior->len);
            if ( wrk->len )
              break;
            prior = prior->next;
          }
        if ( !wrk->len && !webget->ondemand )
          wrk->offs = _Webget_St_Next(st,&wrk->len,0,0);
      }
    
    if ( !wrk->len ) __Auto_Release
      {
        if ( !webget->ondemand || Webget_Done(webget) )
          {
            __Pool_RefPtr(webget);
            wrk->webget = 0;
            --webget->workers_cnt;
            STRICT_REQUIRE( webget->workers_cnt >= 0 );
            
            if ( !webget->workers_cnt )
              {
                if ( webget->callback )
                  webget->callback(webget->cbkobj,WEBGET_FINISHED,0,webget);
              }
          }
        else if ( webget->ondemand )
          {
            Asio_Alert(0,wrk,Cbk_Webget_Alert_Callback);
          } 
      }
    else
      { 
        YOYO_DICTO *hdrs = Httpx_Hdrs_Set_Range(0,wrk->offs,wrk->len);
        wrk->bf->count = 0;
        Httpx_Query(httpx,HTTPX_GET,0,hdrs,0,Buffer_As_File(wrk->bf),st->h.block_size);
      }
  }
#endif
  ;

  



























void _Terminate_All_Wrks(YOYO_WEBGET *webget)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    //
  }
#endif
  ;
  
void _Webget_Abort(YOYO_WEBGET *webget, char *reason)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    __Enter_Once(webget->aborting)
      {
        webget->fail_reason = Str_Copy_Npl(reason,-1);
        _Terminate_All_Wrks(webget);
        while ( webget->prior )
          _Webget_Prior_Failed(webget);
        if ( webget->callback )
          webget->callback(webget->cbkobj,WEBGET_FAILED,0,webget);
      }
  }
#endif
  ;
  
void Cbk_Webget_Alert_Callback(YOYO_WEBGET_WRK *wrk, int status)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    if ( !HTTPX_SUCCEEDED(status) )
      {
        _Webget_Abort(wrk->webget,__yoTa("alert error",0));  
      }
    else    
       Webget_Wrk_Query_Next(wrk,0);
  }
#endif
  ;
  
void Cbk_Webget_Get_Callback(void *obj, YOYO_HTTPX *httpx, int status)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    YOYO_WEBGET_WRK *wrk = obj;
    if ( !HTTPX_SUCCEEDED(status) )
      {
        if ( !httpx->interrupted && !(status&HTTPX_PERMANENT_ERROR))
          {
            Webget_Wrk_Query_Retry(wrk,0);
................................................................................
          }
        else
          {
            _Webget_Abort(wrk->webget,httpx->status_text);
          }
      }
  }
#endif
  ;
  
void Webget_Start_Worker(YOYO_WEBGET *webget)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    if ( !Webget_Done(webget) )
      {
        YOYO_WEBGET_WRK *wrk = __Object_Dtor(sizeof(YOYO_WEBGET_WRK),YOYO_WEBGET_WRK_Destruct);
        wrk->webget = __Refe(webget);
        wrk->bf = __Refe(Buffer_Reserve(0,webget->state?webget->state->h.block_size:0));
        ++webget->workers_cnt;
        Webget_Wrk_Query_Next(wrk,0);
      }
  }
#endif
  ;
  
void _Webget_Start_Workers(YOYO_WEBGET *webget)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    int i;
    if ( Webget_Done(webget) )
      {
        while ( webget->prior ) __Auto_Release
          {
            YOYO_WEBGET_PRIOR *p = __Pool_RefPtr(webget->prior);
................................................................................
        if ( webget->callback )
          webget->callback(webget->cbkobj,WEBGET_FINISHED,0,webget);
      }
    else
      for ( i = 0; i < webget->workers_max; ++i )
        Webget_Start_Worker(webget);
  }
#endif
  ;
  
void _Webget_Query_Head(YOYO_WEBGET *webget);
void Cbk_Webget_Head_Callback(void *obj, YOYO_HTTPX *httpx, int status)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    YOYO_WEBGET *webget = obj;
    if ( !HTTPX_SUCCEEDED(status) )
      {
        if ( !httpx->interrupted && !(status&HTTPX_PERMANENT_ERROR) )
          {
            _Webget_Query_Head(webget);
................................................................................
                if ( webget->callback )
                  webget->callback(webget->cbkobj,WEBGET_BAD_OUTPUT,0,webget);
                _Webget_Abort(webget,__yoTa("bad output",0));
              }
          }
        else
          {
            webget->httpx_fail_status = httpx->status;
            _Webget_Abort(webget,httpx->status_text);
          }
      }
  }
#endif
  ;
  
void _Webget_Query_Head(YOYO_WEBGET *webget)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    YOYO_HTTPX *httpx = Httpx_Client_Callback(webget->source,HTTPX_ASYNC,Cbk_Webget_Head_Callback,webget);
    Httpx_Query(httpx,HTTPX_HEAD,0,0,0,0,0);
  }
#endif
  ;
  
void YOYO_WEBGET_Destruct(YOYO_WEBGET *webget)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    __Unrefe(webget->cbkobj);
    __Unrefe(webget->outstrm);
    if ( webget->state )
      free(webget->state->h.map);
    free(webget->state);
    free(webget->statemap);
................................................................................
    free(webget->output);
    
    while ( webget->prior )
      _Webget_Prior_Failed(webget);
      
    __Destruct(webget);
  }
#endif
  ;
  
YOYO_WEBGET *Webget_Init(
  char *url, char *output, char *statemap, 
  webget_callback_t callback, void *obj,
  int workers)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    YOYO_WEBGET *webget = __Object_Dtor(sizeof(YOYO_WEBGET),YOYO_WEBGET_Destruct);
    webget->statemap = statemap?Str_Copy_Npl(statemap,-1):0;
    webget->output   = output?Str_Copy_Npl(output,-1):0;
    webget->callback = callback;
    webget->cbkobj   = __Refe(obj);
    webget->workers_max = workers>0?workers:WEBGET_WORKERS_COUNT;
    webget->source   = Str_Copy_Npl(url,-1);
    return webget;
  }
#endif
  ;
  
void Webget_Start(YOYO_WEBGET *webget)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    if ( !webget->state )
      {
        if ( !_Webget_Completed_Test(webget) )
          _Webget_Query_Head(webget);
        else
          {
            _Webget_Load_Statemap(webget,-1,0);
            webget->outstrm = __Refe(Cfile_Open(webget->output,"r+"));
            if ( webget->callback )
              webget->callback(webget->cbkobj,WEBGET_STARTED,0,webget);
            if ( webget->callback )
              webget->callback(webget->cbkobj,WEBGET_FINISHED,0,webget);
          }
      }
    else if ( !webget->workers_cnt )
      _Webget_Start_Workers(webget);
  }
#endif
  ;
    
void Webget_Stop(YOYO_WEBGET *webget)
#ifdef _YOYO_WEBGET_BUILTIN
  {
  }
#endif
  ;
  
void Webget_Abort(YOYO_WEBGET *webget)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    _Webget_Abort(webget,__yoTa("manual abort",0));
  }
#endif
  ;
    
YOYO_WEBGET *Webget_Download(char *url, char *output, webget_callback_t callback, void *obj)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    YOYO_WEBGET *webget = 0;
    __Auto_Ptr(webget)
      {
        webget = Webget_Init(url,output,0,callback,obj,WEBGET_WORKERS_COUNT);
        Webget_Start(webget);
      }
    return webget;
  }
#endif
  ;
  
YOYO_WEBGET *Webget_Reget(char *url, char *output, char *statemap, webget_callback_t callback, void *obj)
#ifdef _YOYO_WEBGET_BUILTIN
  {
    YOYO_WEBGET *webget = 0;
    __Auto_Ptr(webget)
      {
        webget = Webget_Init(url,output,statemap,callback,obj,WEBGET_WORKERS_COUNT);
        Webget_Start(webget);
      }
    return webget;
  }
#endif
  ;

#endif /*C_once_A680A0EB_B8EE_4F96_B06B_74BA1C9C8136*/

Changes to webhost.hc.

334
335
336
337
338
339
340

341
342
343
344
345
346
347
348

349

350

351
352
353
354
355
356
357
358
359
360

361

362
363
364
365
366
367
368
...
458
459
460
461
462
463
464

465
466
467
468
469
470
471
...
517
518
519
520
521
522
523
524

525
526



527





















528
529
530
531
532
533
534
535
536
537
538
    YOYO_PIPEX *pipex;
    char *path;
    char *script;
    char *pathinfo;
  } YOYO_WEBHOST_CGIST;

void YOYO_WEBHOST_CGIST_Destruct(YOYO_WEBHOST_CGIST *st)

  {
    __Unrefe(st->host);
    __Unrefe(st->url);
    __Unrefe(st->rqst);
    __Unrefe(st->pipex);
    free(st->path);
    __Destruct(st);
  }



YOYO_WEBHOST_CGIST *Webhost_CGI_State(YOYO_WEBHOST *host, YOYO_HTTPD_REQUEST *rqst, char *path, char *script, char *pathinfo, YOYO_URL *url)

  {
    YOYO_WEBHOST_CGIST *st = __Object_Dtor(sizeof(YOYO_WEBHOST_CGIST),YOYO_WEBHOST_CGIST_Destruct);
    st->host = __Refe(host);
    st->url  = __Refe(url);
    st->rqst = __Refe(rqst);
    st->path = path;
    st->script = script;
    st->pathinfo = pathinfo;
    return st;
  }

  

void Webhost_Check_CGI_Status(YOYO_WEBHOST_CGIST *st, int status)
#ifdef _YOYO_WEBHOST_BUILTIN
  {
    int retcode = 0;
    
    if ( !ASIO_ST_SUCCEEDED(status) ) return;
    
................................................................................
    else if ( status == HTTPD_RQST_POSTDTA )
      return HTTPD_ACCEPT;
    else if ( status == HTTPD_RQST_PERFORM )
      {
        YOYO_FILE_STATS stats = {0};
        YOYO_URL *url = Url_Parse_Uri(rqst->uri);
        char *path = 0;


        Log_Info("[%p] %s/1.%d %s",rqst,Httpd_Method_String(rqst->method),rqst->httpver,rqst->uri);
        Logout_Debug(Dicto_Format(rqst->qhdr,Buffer_Print,0,1));
        
        if ( rqst->instrm_length && Log_Level(YOYO_LOG_INFO) )
          {
            enum { MAX_L = 60 };
................................................................................
                    rqst->outstrm_length = obf->count;
                    rqst->outstrm_type = Mime_String_Of_Npl(YOYO_MIME_HTML);
                    return HTTPD_SUCCESS;
                  }
                return HTTPD_NOTFOUND;
              }
          open_file:  
            rqst->outstrm = __Refe(Cfile_Open(path,"r"));

            rqst->outstrm_type = Str_Copy_Npl(Webhost_Conenttype_Of(path),-1);
            rqst->outstrm_length = (int)stats.length;



            return HTTPD_SUCCESS;





















          }
        else
          return HTTPD_NOTFOUND;
      }
    
    return HTTPD_REJECT;
  }
#endif
  ;

#endif /*C_once_47E24D4B_C225_4002_B318_6FB9DDE274BE*/







>








>
|
>

>










>
|
>







 







>







 







<
>


>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>











334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
...
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
...
524
525
526
527
528
529
530

531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
    YOYO_PIPEX *pipex;
    char *path;
    char *script;
    char *pathinfo;
  } YOYO_WEBHOST_CGIST;

void YOYO_WEBHOST_CGIST_Destruct(YOYO_WEBHOST_CGIST *st)
#ifdef _YOYO_WEBHOST_BUILTIN
  {
    __Unrefe(st->host);
    __Unrefe(st->url);
    __Unrefe(st->rqst);
    __Unrefe(st->pipex);
    free(st->path);
    __Destruct(st);
  }
#endif
  ;
  
YOYO_WEBHOST_CGIST *Webhost_CGI_State(YOYO_WEBHOST *host, YOYO_HTTPD_REQUEST *rqst, char *path, char *script, char *pathinfo, YOYO_URL *url)
#ifdef _YOYO_WEBHOST_BUILTIN
  {
    YOYO_WEBHOST_CGIST *st = __Object_Dtor(sizeof(YOYO_WEBHOST_CGIST),YOYO_WEBHOST_CGIST_Destruct);
    st->host = __Refe(host);
    st->url  = __Refe(url);
    st->rqst = __Refe(rqst);
    st->path = path;
    st->script = script;
    st->pathinfo = pathinfo;
    return st;
  }
#endif
  ;
    
void Webhost_Check_CGI_Status(YOYO_WEBHOST_CGIST *st, int status)
#ifdef _YOYO_WEBHOST_BUILTIN
  {
    int retcode = 0;
    
    if ( !ASIO_ST_SUCCEEDED(status) ) return;
    
................................................................................
    else if ( status == HTTPD_RQST_POSTDTA )
      return HTTPD_ACCEPT;
    else if ( status == HTTPD_RQST_PERFORM )
      {
        YOYO_FILE_STATS stats = {0};
        YOYO_URL *url = Url_Parse_Uri(rqst->uri);
        char *path = 0;
        int rng_pos, rng_len;

        Log_Info("[%p] %s/1.%d %s",rqst,Httpd_Method_String(rqst->method),rqst->httpver,rqst->uri);
        Logout_Debug(Dicto_Format(rqst->qhdr,Buffer_Print,0,1));
        
        if ( rqst->instrm_length && Log_Level(YOYO_LOG_INFO) )
          {
            enum { MAX_L = 60 };
................................................................................
                    rqst->outstrm_length = obf->count;
                    rqst->outstrm_type = Mime_String_Of_Npl(YOYO_MIME_HTML);
                    return HTTPD_SUCCESS;
                  }
                return HTTPD_NOTFOUND;
              }
          open_file:  

          
            rqst->outstrm_type = Str_Copy_Npl(Webhost_Conenttype_Of(path),-1);
            rqst->outstrm_length = (int)stats.length;

            if ( rqst->method == HTTPD_HEAD_METHOD )
              {
                return HTTPD_SUCCESS;
              }
            else
              {
                rqst->outstrm = __Refe(Cfile_Open(path,"r"));
                if ( Httpd_Rqst_Range(rqst,&rng_pos,&rng_len) )
                  {
                    int L = (int)stats.length;
                    if ( L > rng_pos && rng_len > 0 )
                      {
                        rng_len = Yo_Mini(rng_len,L-rng_pos);
                        Oj_Seek(rqst->outstrm,rng_pos,0);
                        rqst->outstrm_length = rng_len;
                        Httpd_Set_Content_Range(rqst,rng_pos,rng_len,(int)stats.length);
                        return HTTPD_PARTIAL;
                      }
                    else
                      return 416; // Requested Range Not Satisfiable
                  }
                else
                  return HTTPD_SUCCESS;
              }
          }
        else
          return HTTPD_NOTFOUND;
      }
    
    return HTTPD_REJECT;
  }
#endif
  ;

#endif /*C_once_47E24D4B_C225_4002_B318_6FB9DDE274BE*/