File: lib-examples.rst

package info (click to toggle)
davix 0.8.10-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,184 kB
  • sloc: ansic: 164,612; cpp: 38,741; python: 17,726; perl: 14,124; sh: 13,458; xml: 3,567; makefile: 1,959; javascript: 885; pascal: 570; lisp: 7
file content (707 lines) | stat: -rw-r--r-- 18,802 bytes parent folder | download | duplicates (5)
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
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
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
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
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
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
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
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
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
645
646
647
648
649
650
651
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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
Using libdavix
==============

File interface
--------------

Most methods in the ``Davix::DavFile`` API come with an exception safe version
which accepts a ``DavixError*`` as an argument, and a version which throws ``DavixException``
instead.

* Instantiate a new DavFile.

.. code-block:: cpp

    Context c;
    DavFile file(c, Uri("http://example.org/dir1/file1"));

* Get all replicas associated to a file.

.. code-block:: cpp

    Context c;
    DavixError* err = NULL;
    DavFile file(c, Uri("http://example.org/dir1/file1"));
    std::vector<DavFile> resultVec;
    resultVec = file.getReplicas(NULL, &err);

* Execute a vector read operation.

.. code-block:: cpp

    Context c;
    DavixError* err = NULL;
    DavFile file(c, Uri("http://example.org/dir1/file_to_read"));

    int number_of_vector = 2;
    DavIOVecInput input_vector[number_of_vector];
    DavIOVecOutput output_vector[number_of_vector];

    // Setup vector operations parameters
    char buf1[255] = {0};
    char buf2[255] = {0};

    input_vector[0].diov_offset = 100;
    input_vector[0].diov_size = 200;
    input_vector[0].diov_buffer = buf1;

    input_vector[1].diov_offset = 600;
    input_vector[1].diov_size = 150;
    input_vector[1].diov_buffer = buf2;

    // execute query
    file.readPartialBufferVec(NULL, input_vector, output_vector, number_of_vector, &err);

    std::cout << "Op 1 read " << output_vector[0].diov_size << "bytes" << std::endl;
    std::cout << "Op 2 read " << output_vector[1].diov_size << "bytes" << std::endl;

    // do things with content in output_vector[0].diov_buffer etc

* Write the contents of a remote file to a local file descriptor.

.. code-block:: cpp

    Context c;
    DavixError* err = NULL;
    DavFile file(c, Uri("http://example.org/dir1/file_to_download"));
    int fd = open("/tmp/local_file" O_WRONLY, O_CREAT);
    // get full file
    file.getToFd(NULL, fd, &err);

    // get first 200 bytes from file
    file.getToFd(NULL, fd, 200, &err);

* Download parts of a file with a single-range GET

.. code-block:: cpp

    Context c;
    char buffer[255];
    DavFile file(c, Uri("http://example.org/dir1/file_to_download"));

    // get 100 bytes from http://example.org/dir1/file_to_download at offset 200
    file.readPartial(NULL, buffer, 100, 200);

* Download full file contents to a dynamically allocated buffer.

.. code-block:: cpp

    Context c;
    DavixError* err = NULL;
    DavFile file(c, Uri("http://example.org/file_to_download"));
    std::vector<char> buffer;

    // warning, this operation has no size limit regarding the content
    file.getFull(NULL, buffer, &err);

    // do things with buffer
    // ...

* Create or replace a remote file with the contents of a file descriptor.

.. code-block:: cpp

    Context c;
    DavFile file(c, Uri("http://example.org/file_to_create"));

    int fd = open("/tmp/file_to_upload", O_RDONLY);

    // get file size
    struct stat st;
    fstat(fd, &st);

    // execute put
    file.put(NULL, fd, static_cast<dav_size_t>(st.st_size));

* Create or replace a remote file with the contents of a buffer.

.. code-block:: cpp

    Context c;
    DavFile file(c, Uri("http://example.org/file_to_create"));

    char buffer[255];

    // fills buffer with something useful

    // execute put
    file.put(NULL, &buffer, static_cast<dav_size_t>sizeof(buffer));

* Create or replace a remote file with contents provided by a callback function.

.. code-block:: cpp

    // data provider
    int myDataProvider(void* buffer, dav_size_t max_size){
        static dav_size_t content_size = 200;
        if(max_size == 0)
            return 0;
        else{
            char my_useful_content[255]={1};
            int bytes_to_write = (max_size<content_size)?max_size:content_size;

            memcpy(buffer, my_useful_content, bytes_to_write);

            content_size -= bytes_to_write;
            return bytes_to_write;
        }
    }

    int main(int argc, char** argv){
        Context c;
        DavFile file(c, Uri("http://example.org/file_to_create"));

        // set data provider callback
        DataProviderFun dataCB = myDataProvider;

        // execute put and write 100 bytes using data from callback
        file.put(NULL, dataCB, 100);
    }

* Move a remote resource to another location.

.. code-block:: cpp

    Context c;
    DavFile source(c, Uri("http://example.org/old_location"));
    DavFile destination(c, Uri("http://example.org/new_location"));

    source.move(NULL, destination);

* Delete a collection or a directory.

.. code-block:: cpp

    Context c;
    DavixError* err = NULL;

    // delete a WebDAV collection
    DavFile myDavCollection(c, Uri("davs://example.org/collection_to_delete"));
    myDavCollection.deletion(NULL, &err);

    // to delete a S3 bucket (note: bucket has to be empty or operation will fail)
    // setup S3 authorisation keys
    RequestParams params;
    params.setAwsAuthorizationKeys("xxxxx", "yyyyy");
    DavFile myS3Bucket(c, Uri("s3://bucket_to_delete.example.org"));
    myS3Bucket.deletion(&params, &err);

* Create a collection or directory.

.. code-block:: cpp

    Context c;
    DavixError* err = NULL;
    // Instantiate RequestParams object to hold request options
    RequestParams params;

    // to create a WebDav collection
    DavFile myDavCollection(c, Uri("dav://example.org/collection_to_create"));
    myDavCollection.makeCollection(NULL, &err);

    // to create a new S3 bucket
    // first we need to setup S3 authorisation keys for this request
    params.setAwsAuthorizationKeys("xxxxx", "yyyyy");
    DavFile myS3Bucket(c, Uri("s3://bucket_to_create.example.org"));
    myS3Bucket.makeCollection(&params, &err);

* Query basic file metadata.

.. code-block:: cpp

    Contect c;
    DavFile file(c, Uri("http://example.org/dir/file_to_stat"));

    StatInfo info;
    file.stat(NULL, info);
    std::cout << "my file is " << info.size << " bytes large " << std::endl;
    std::cout << " mode : 0" << std::oct << info.mode << std::endl;
    std::cout << " atime : " << info.atime << std::endl;
    std::cout << " mtime : " << info.mtime << std::endl;
    std::cout << " ctime : " << info.ctime << std::endl;

* List the contents of a collection.

.. code-block:: cpp

    Contect c;
    DavFile file(c, Uri("http://example.org/collection_to_list"));

    DavFile::Iterator it = file.listCollection(NULL);

    // prints out entries' name
    do {
        std::cout << it.name() << std::endl;
    }while(it.next());

* Calculate a checksum

.. code-block:: cpp

    Context c;
    DavFile file(c, Uri("http://example.org/file_to_checksum"));
    std::string chk;

    // calculate MD5, also supports CRC32, ADLER32
    file.checksum(NULL, chk, "MD5", &err);
    std::cout << "MD5 " << chk << std::endl;

POSIX interface
---------------

* Instantiate a new DavPosix

.. code-block:: cpp

    Context c;
    DavPosix pos(&c);

* Query basic file metadata

.. code-block:: cpp

    Contect c;
    DavixError* err = NULL;
    DavPosix pos(&c);
    struct stat info;

    pos.stat(NULL, "http://example.org/file_to_stat", &info, &err);

    std::cout << " atime : " << info.st_atime << std::endl;
    std::cout << " mtime : " << info.st_mtime << std::endl;
    std::cout << " ctime : " << info.st_ctime << std::endl;
    std::cout << " mode : 0" << std::oct << info.st_mode << std::endl;
    std::cout << " len : " << info.st_size << std::endl;

* Open and read a collection

.. code-block:: cpp

    Context c;
    DavixError* err = NULL;
    DavPosix pos(&c);

    DAVIX_DIR* fd;
    struct dirent* entry;

    fd = pos.opendir(NULL, "dav://example.org/collection_to_open", &err);

    while(entry = pos.readdir(fd, &err)){
        std::cout << entry->d_name << std::endl;
    }

    pos.closedir(fd, &err);

* Open and read a collection with per-entry metadata

.. code-block:: cpp

    Context c;
    DavixError* err = NULL;
    DavPosix pos(&c);

    DAVIX_DIR* fd;
    struct dirent* entry;
    struct stat info;

    fd = posix.opendirpp(NULL, "dav://example.org/collection_to_open", &err);

    while(entry = pos.readdirpp(fd, &info, &err)){
        std::cout << entry->d_name << "is " << info.st_size << "bytes in size." << std::endl;
    }

    pos.closedirpp(fd, &err);

* Create a collection or directory

.. code-block:: cpp

    Context c;
    DavixError* err = NULL;
    DavPosix pos(&c);

    pos.mkdir(NULL, "dav://example.org/collection_to_create", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH, &err);

* Rename a file or collection

.. code-block:: cpp

    Context c;
    DavixError* err = NULL;
    DavPosix pos(&c);

    pos.rename(NULL, "http://example.org/myfolder/old_file_name", "http://example.org/myfolder/new_file_name", &err);

* Remove a file

.. code-block:: cpp

    Context c;
    DavixError* err = NULL;
    DavPosix pos(&c);

    pos.unlink(NULL, "http://example.org/file_to_delete", &err);

* Remove a collection or directory

.. code-block:: cpp

    Context c;
    DavixError* err = NULL;
    DavPosix pos(&c);

    pos.rmdir(NULL, "dav://example.org/collection_to_remove", &err);

* Open a file for random I/O (read, partial read, and write)

.. code-block:: cpp

    Context c;
    DavixError* err = NULL;
    DavPosix pos(&c);

    DAVIX_FD* fd;
    fd = pos.open(NULL, "http://example.org/myfile", O_RDONLY, &err);

    // read 200 bytes from myfile
    char buffer[255];
    pos.read(fd, &buffer, 200, &err);

    // read 50 bytes from myfile at offset 100
    char buffer2[255];
    pos.pread(fd, &buffer2, 50, 100, &err);
    pos.close(fd);

    // create a new file and write 200 bytes from buffer to it
    fd = pos.open(NULL, "http://example.org/myfolder/mynewfile", O_WRONLY | O_CREAT, &err);
    pos.write(fd, &buffer, 200);
    pos.close(fd);

* Vectored read - carry out several read operations in one single request, if the server supports it

.. code-block:: cpp

    Context c;
    DavixError* err = NULL;
    DavPosix pos(&c);

    int number_of_vector = 2;
    DavIOVecInput input_vector[number_of_vector];
    DavIOVecOuput output_vector[number_of_vector];

    DAVIX_FD* fd;
    fd = pos.open(NULL, "http://example.org/myfile", O_RDONLY, &err);

    // Setup vector operations parameters
    char buf1[255] = {0};
    char buf2[255] = {0};

    input_vector[0].diov_offset = 100;
    input_vector[0].diov_size = 200;
    input_vector[0].diov_buffer = buf1;

    input_vector[1].diov_offset = 600;
    input_vector[1].diov_size = 150;
    input_vector[1].diov_buffer = buf2;

    // execute query
    pos.preadVec(fd, input_vector, output_vector, number_of_vector, &err);

    std::cout << "Op 1 read " << output_vector[0].diov_size << "bytes" << std::endl;
    std::cout << "Op 2 read " << output_vector[1].diov_size << "bytes" << std::endl;

    // do things with content in output_vector[0].diov_buffer etc

    pos.close(fd);

* Re-position read/write file offset

.. code-block:: cpp

    Context c;
    DavixError* err = NULL;
    DavPosix pos(&c);

    DAVIX_FD* fd;
    fd = pos.open(NULL, "http://example.com/myfile", O_RDONLY, &err);

    // position cursor to 200 bytes offset
    lseek(fd, 200, SEEK_SET, &err);

    // position cursor to current location plus 100 offset
    lseek(fd, 100, SEEK_CUR, &err);

    // position cursor to end of the file plus offset 200
    lseek(fd, 200, SEEK_END, &err);

    pos.close(fd);

HTTP requests
-------------

The ``Davix::HttpRequest`` interface allows you to construct, customise and execute HTTP requests.
It also provides methods for retrieving server responses.

Requests can be executed in two ways:

* Using the ``executeRequest()`` method, which will execute the entire request immediately.
* Using the ``beginRequest()`` function, which will initiate a multi-part request. This should be used for requests that expect a large answer.
  Note that requests initiated by ``beginRequest()`` should be closed by using the ``endRequest()``.

* Instantiate a new HttpRequest

.. code-block:: cpp

    Context c;
    DavixError* err = NULL;
    HttpRequest myrequest(c, "http://example.org/some_useful_stuff", &err);

* Set the request method

.. code-block:: cpp

    myrequest.setRequestMethod("GET");

* Configure request parameters

.. code-block:: cpp

    RequestParams params;
    params.setUserAgent("MyAwesomeApp");
    params.setClientLoginPassword("my_login_name", "my_uber_secure_password");
    // ...
    myrequest.setParameters(params);

* Add custom header field

.. code-block:: cpp

    myrequest.addHeaderField("Accept", "application/metalink4+xml");

* Set the request body

.. code-block:: cpp

    // from a string
    std::string content_string
    myrequest.setRequestBody(content_string);

    // from a buffer
    char buffer [255];
    // fills buffer with something useful
    myrequest.setRequestBody(&buffer, sizeof(buffer));

    // from a file descriptor, at offset 100 for 200 bytes
    int fd = open("/tmp/myfile", O_RDONLY);
    myrequest.setRequestBody(fd, 100, 200);
    close(fd);

* Execute a full request

.. code-block:: cpp

    myrequest.executeRequest(&err);

* Start a multi-part HTTP request

.. code-block:: cpp

    myrequest.beginRequest(&err);

* End a multi-part HTTP request

.. code-block:: cpp

    myrequest.endRequest(&err);

* Read a block of size n bytes from the answer

.. code-block:: cpp

    // read max n bytes to static buffer
    char buffer[255];
    myrequest.readBlock(&buffer, n, &err);

    // read to dynamically sized buffer, with max size n
    std::vector<char> buffer2;
    myrequest.readBlock(&buffer2, n, &err);

* Read a segment of size n from the answer

.. code-block:: cpp

    // readSegment calls readBlock repeatedly until n size is read, or end of answer
    char buffer[50*1024];
    myrequest.readSegment(&buffer, n, &err);

* Read a line of text from the answer, with a maximum size of n

.. code-block:: cpp

    char buffer[255];
    myrequest.readLine(&buffer, n, &err);

* Write the answer contents to a file descriptor

.. code-block:: cpp

    char buffer[255]
    int fd = open("tmp/myfile", O_WRONLY | O_CREAT);

    // with no size limit
    myrequest.readToFd(fd, &err);

    // with 100 bytes limit
    myrequest.readToFd(fd, 100, &err);

* Get size of answer

.. code-block:: cpp

    dav_ssize_t size;
    size = myrequest.getAnswerSize();

* Get response body

.. code-block:: cpp

    // into dynamically sized buffer
    std::vector<char> buffer1;
    buffer1 = myrequest.getAnswerContentVec();

    // into static buffer
    const char* buffer2 = myrequest.getAnswerContent();

* Get the status code of the response

.. code-block:: cpp

    int code;
    code = myrequest.getRequestCode();

* Get last modified time

.. code-block:: cpp

    time_t last_modified;
    last_modified = myrequest.getLastModified();

* Get the value associated with a particular header key

.. code-block:: cpp

    std::string value;
    myrequest.getAnswerHeader("Content-Type", &value);
    std::cout << "Content-Type is " << value << std::endl;

* Get all header fields into a vector

.. code-block:: cpp

    HeaderVec headers;
    myrequest.getAnswerHeaders(headers);
    for(HeaderVec::iterator it = headers.begin(), it < headers.end(); ++it){
        std::cout << it->first << ": " << it->second << std::endl;
    }

* Clear the HttpRequest answer buffer

.. code-block:: cpp

    myrequest.clearAnswerContent();

* Process then discard the response body

.. code-block:: cpp

    // calls readSegment on the answer repeatedly but do nothing with content
    myrequest.discardBody(&err);

* Use pre-configured requests for specific HTTP operations

.. code-block:: cpp

    Context c;
    DavixError* err = NULL;
    Uri myuri("http://example.org/myfile");

    // Get request
    GetRequest req(c, myuri, &err);

    // Put request
    PutRequest req(c, myuri, &err);

    // Head request
    HeadRequest req(c, myuri, &err);

    // Delete request
    DeleteRequest req(c, myuri, &err);

    // Propfind request
    ProfindRequest req(c, myuri, &err);

Error reporting and exceptions
------------------------------

Davix uses an error reporting system similar to Glib, relying on ``Davix::DavixError`` objects to store the scope where an error occurred,
the error code, and the error message.

The ``DavixError`` objects can be checked on local level, or passed into other functions for error handling, or be propagated back up the stack frame.

Davix also provides a ``Davix::DavixException`` class that encapsulates ``DavixError``, for situations where exceptions are more appropriate.

Most functions in the ``Davix::DavFile`` API provide an exception safe version that takes a ``DavixError*`` as argument, as well as a version that throws ``DavixException`` and does not require ``DavixError``.

Davix::DavixError
~~~~~~~~~~~~~~~~~

The ``DavixError`` error reporting system can be used when exception throwing behavior is not desirable.

A ``DavixError`` object should not be instantiated manually, the ``DavixError::setupError()`` function should be used where an error has occurred.

Most functions provided by the APIs accept a pointer to the ``DavixError`` type, which if set to ``NULL``, will bypasses the error reporting system. In the event where an error occurs, a new ``DavixError`` object is created and its address assigned to the ``DavixError*`` passed into the function.

.. code-block:: cpp

    Context c;
    DavixError* err = NULL;

    // delete a WebDAV collection
    DavFile myDavCollection(c, Uri("davs://example.org/collection_to_delete"));
    myDavCollection.deletion(NULL, &err);

    // check if an error occurred
    // if err is not NULL anymore, that means a DavixError object had been created
    // if *err is not NULL, the object is valid
    if(err && *err){
        std::cerr << err->getErrScope() <<
                     " Error code: " << err->getStatus() <<
                     " Error: " << err->getErrMsg() << std::endl;
    }

Davix::DavixException
~~~~~~~~~~~~~~~~~~~~~

``DavixException`` is the recommended error handling method when using the ``DavFile`` API, it encapsulates a ``DavixError`` object which holds information such as the scope where an error occurred, the error code, and the error message.

* Throw a ``DavixException``

.. code-block:: cpp

    throw DavixException("Where", "What", "This is what has happened...");

* Catch a ``DavixException``

.. code-block:: cpp

    TRY_DAVIX{
        DavFile myDavCollection(c, Uri("davs://example.org/doomed_to_fail"));
            myDavCollection.deletion(NULL);
    }CATCH_DAVIX(&err){
        std::cerr << err->getErrScope() <<
        " Error code: " << err->getStatus() <<
        " Error: " << err->getErrMsg() << std::endl;

        // handle error or propagate
        // ...
    }