aboutsummaryrefslogtreecommitdiff
path: root/bbot/manifest.cxx
blob: 1d164db471f7c56f8c3da7e124b9610aac2ab65b (plain)
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
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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
// file      : bbot/manifest.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <bbot/manifest>

#include <vector>
#include <string>
#include <cctype>    // isxdigit()
#include <cassert>
#include <sstream>
#include <cstddef>   // size_t
#include <utility>   // move()
#include <cstdint>   // uint64_t
#include <stdexcept> // invalid_argument

#include <butl/utility>             // digit()
#include <butl/tab-parser>
#include <butl/string-parser>
#include <butl/manifest-parser>
#include <butl/manifest-serializer>

using namespace std;
using namespace butl;
using namespace bpkg;

namespace bbot
{
  using parser = manifest_parser;
  using parsing = manifest_parsing;
  using serializer = manifest_serializer;
  using serialization = manifest_serialization;
  using name_value = manifest_name_value;

  using strings = vector<string>;

  // result_status
  //
  string
  to_string (result_status s)
  {
    switch (s)
    {
    case result_status::success:  return "success";
    case result_status::warning:  return "warning";
    case result_status::error:    return "error";
    case result_status::abort:    return "abort";
    case result_status::abnormal: return "abnormal";
    }

    assert (false);
    return string ();
  }

  result_status
  to_result_status (const string& s)
  {
         if (s == "success")  return result_status::success;
    else if (s == "warning")  return result_status::warning;
    else if (s == "error")    return result_status::error;
    else if (s == "abort")    return result_status::abort;
    else if (s == "abnormal") return result_status::abnormal;
    else throw invalid_argument ("invalid result status '" + s + "'");
  }

  // Utility functions
  //
  inline static bool
  valid_sha256 (const string& s) noexcept
  {
    if (s.size () != 64)
      return false;

    for (const auto& c: s)
    {
      if ((c < 'a' || c > 'f' ) && !digit (c))
        return false;
    }

    return true;
  }

  inline static bool
  valid_fingerprint (const string& f) noexcept
  {
    size_t n (f.size ());
    if (n != 32 * 3 - 1)
      return false;

    for (size_t i (0); i < n; ++i)
    {
      char c (f[i]);
      if ((i + 1) % 3 == 0)
      {
        if (c != ':')
          return false;
      }
      else if (!isxdigit (c))
        return false;
    }

    return true;
  }

  // machine_header_manifest
  //
  machine_header_manifest::
  machine_header_manifest (parser& p, bool iu)
      : machine_header_manifest (p, p.next (), iu)
  {
    // Make sure this is the end.
    //
    name_value nv (p.next ());
    if (!nv.empty ())
      throw parsing (p.name (), nv.name_line, nv.name_column,
                     "single machine header manifest expected");
  }

  machine_header_manifest::
  machine_header_manifest (parser& p, name_value nv, bool iu)
  {
    auto bad_name = [&p, &nv] (const string& d)
    {
      throw parsing (p.name (), nv.name_line, nv.name_column, d);
    };

    auto bad_value = [&p, &nv] (const string& d)
    {
      throw parsing (p.name (), nv.value_line, nv.value_column, d);
    };

    // Make sure this is the start and we support the version.
    //
    if (!nv.name.empty ())
      bad_name ("start of machine header manifest expected");

    if (nv.value != "1")
      bad_value ("unsupported format version");

    for (nv = p.next (); !nv.empty (); nv = p.next ())
    {
      string& n (nv.name);
      string& v (nv.value);

      if (n == "id")
      {
        if (!id.empty ())
          bad_name ("machine id redefinition");

        if (v.empty ())
          bad_value ("empty machine id");

        id = move (v);
      }
      else if (n == "name")
      {
        if (!name.empty ())
          bad_name ("machine name redefinition");

        if (v.empty ())
          bad_value ("empty machine name");

        name = move (v);
      }
      else if (n == "summary")
      {
        if (!summary.empty ())
          bad_name ("machine summary redefinition");

        if (v.empty ())
          bad_value ("empty machine summary");

        summary = move (v);
      }
      else if (!iu)
        bad_name ("unknown name '" + n + "' in machine header manifest");
    }

    // Verify all non-optional values were specified.
    //
    if (id.empty ())
      bad_value ("no machine id specified");

    if (name.empty ())
      bad_value ("no machine name specified");

    if (summary.empty ())
      bad_value ("no machine summary specified");
  }

  void machine_header_manifest::
  serialize (serializer& s) const
  {
    // @@ Should we check that all non-optional values are specified and all
    //    values are valid?
    //
    s.next ("", "1"); // Start of manifest.
    s.next ("id", id);
    s.next ("name", name);
    s.next ("summary", summary);
    s.next ("", ""); // End of manifest.
  }

  // task_request_manifest
  //
  task_request_manifest::
  task_request_manifest (parser& p, bool iu)
  {
    name_value nv (p.next ());

    auto bad_name = [&p, &nv] (const string& d)
    {
      throw parsing (p.name (), nv.name_line, nv.name_column, d);
    };

    auto bad_value = [&p, &nv] (const string& d)
    {
      throw parsing (p.name (), nv.value_line, nv.value_column, d);
    };

    // Make sure this is the start and we support the version.
    //
    if (!nv.name.empty ())
      bad_name ("start of task request manifest expected");

    if (nv.value != "1")
      bad_value ("unsupported format version");

    // Parse the task request manifest.
    //
    for (nv = p.next (); !nv.empty (); nv = p.next ())
    {
      string& n (nv.name);
      string& v (nv.value);

      if (n == "agent")
      {
        if (!agent.empty ())
          bad_name ("task request agent redefinition");

        if (v.empty ())
          bad_value ("empty task request agent");

        agent = move (v);
      }
      else if (n == "fingerprint")
      {
        if (fingerprint)
          bad_name ("task request fingerprint redefinition");

        if (!valid_sha256 (v))
          bad_value ("invalid task request fingerprint");

        fingerprint = move (v);
      }
      else if (!iu)
        bad_name ("unknown name '" + n + "' in task request manifest");
    }

    // Verify all non-optional values were specified.
    //
    if (agent.empty ())
      bad_value ("no task request agent specified");

    // Parse machine header manifests.
    //
    for (nv = p.next (); !nv.empty (); nv = p.next ())
      machines.emplace_back (machine_header_manifest (p, nv, iu));

    if (machines.empty ())
      bad_value ("no task request machines specified");
  }

  void task_request_manifest::
  serialize (serializer& s) const
  {
    // @@ Should we check that all non-optional values are specified and all
    //    values are valid?
    //
    s.next ("", "1"); // Start of manifest.
    s.next ("agent", agent);

    if (fingerprint)
      s.next ("fingerprint", *fingerprint);

    s.next ("", "");  // End of manifest.

    for (const machine_header_manifest& m: machines)
      m.serialize (s);

    s.next ("", ""); // End of stream.
  }

  // task_manifest
  //
  task_manifest::
  task_manifest (parser& p, bool iu)
      : task_manifest (p, p.next (), iu)
  {
    // Make sure this is the end.
    //
    name_value nv (p.next ());
    if (!nv.empty ())
      throw parsing (p.name (), nv.name_line, nv.name_column,
                     "single task manifest expected");
  }

  task_manifest::
  task_manifest (parser& p, name_value nv, bool iu)
  {
    auto bad_name = [&p, &nv] (const string& d)
    {
      throw parsing (p.name (), nv.name_line, nv.name_column, d);
    };

    // Offsets are used to tie an error to the specific position inside a
    // manifest value (possibly a multiline one).
    //
    auto bad_value = [&p, &nv] (
      const string& d, uint64_t column_offset = 0, uint64_t line_offset = 0)
    {
      throw parsing (p.name (),
                     nv.value_line + line_offset,
                     (line_offset == 0 ? nv.value_column : 1) + column_offset,
                     d);
    };

    // Make sure this is the start and we support the version.
    //
    if (!nv.name.empty ())
      bad_name ("start of task manifest expected");

    if (nv.value != "1")
      bad_value ("unsupported format version");

    // Parse the task manifest.
    //
    for (nv = p.next (); !nv.empty (); nv = p.next ())
    {
      string& n (nv.name);
      string& v (nv.value);

      if (n == "name")
      {
        if (!name.empty ())
          bad_name ("task package name redefinition");

        if (v.empty ())
          bad_value ("empty task package name");

        name = move (v);
      }
      else if (n == "version")
      {
        if (!version.empty ())
          bad_name ("task package version redefinition");

        try
        {
          version = bpkg::version (move (v));
        }
        catch (const invalid_argument& e)
        {
          bad_value (string ("invalid task package version: ") + e.what ());
        }

        // Versions like 1.2.3- are forbidden in manifest as intended to be
        // used for version constrains rather than actual releases.
        //
        if (version.release && version.release->empty ())
          bad_value ("invalid task package version release");
      }
      else if (n == "repository")
      {
        if (!repository.empty ())
          bad_name ("task repository redefinition");

        if (v.empty ())
          bad_value ("empty task repository");

        try
        {
          // Call remote/absolute repository location constructor (throws
          // invalid_argument for relative location).
          //
          repository = repository_location (move (v));
        }
        catch (const invalid_argument& e)
        {
          bad_value (string ("invalid task repository: ") + e.what ());
        }
      }
      else if (n == "trust")
      {
        if (v != "yes" && !valid_fingerprint (v))
          bad_value ("invalid repository certificate fingerprint");

        trust.emplace_back (move (v));
      }
      else if (n == "machine")
      {
        if (!machine.empty ())
          bad_name ("task machine redefinition");

        if (v.empty ())
          bad_value ("empty task machine");

        machine = move (v);
      }
      else if (n == "target")
      {
        if (target)
          bad_name ("task target redefinition");

        try
        {
          target = target_triplet (v);
        }
        catch (const invalid_argument& e)
        {
          bad_value (string ("invalid task target: ") + e.what ());
        }
      }
      else if (n == "config")
      {
        if (!config.empty ())
          bad_name ("task configuration redefinition");

        // Note that when reporting errors we combine the manifest value
        // position with the respective field and error positions.
        //
        try
        {
          istringstream is (v);
          tab_parser parser (is, "");

          // Here we naturally support multiline config manifest.
          //
          tab_fields tl;
          while (!(tl = parser.next ()).empty ())
          {
            for (auto& tf: tl)
            {
              try
              {
                check_config (tf.value);
              }
              catch (const invalid_argument& e)
              {
                bad_value (string ("invalid task configuration: ") + e.what (),
                           tf.column - 1,
                           tl.line - 1);
              }

              config.emplace_back (move (tf.value));
            }
          }
        }
        catch (const tab_parsing& e)
        {
          bad_value ("invalid task configuration: " + e.description,
                     e.column - 1,
                     e.line - 1);
        }

        if (config.empty ())
          bad_value ("empty task configuration");
      }
      else if (!iu)
        bad_name ("unknown name '" + n + "' in task manifest");
    }

    // Verify all non-optional values were specified.
    //
    if (name.empty ())
      bad_value ("no task package name specified");

    if (version.empty ())
      bad_value ("no task package version specified");

    if (repository.empty ())
      bad_value ("no task repository specified");

    if (machine.empty ())
      bad_value ("no task machine specified");
  }

  void task_manifest::
  serialize (serializer& s) const
  {
    // @@ Should we check that all non-optional values are specified and all
    //    values are valid?
    //
    s.next ("", "1"); // Start of manifest.
    s.next ("name", name);
    s.next ("version", version.string ());
    s.next ("repository", repository.string ());

    for (const auto& v: trust)
      s.next ("trust", v);

    s.next ("machine", machine);

    if (target)
      s.next ("target", target->string ());

    // Recompose config string as a space-separated variable list,
    //
    if (!config.empty ())
    {
      string v;
      for (auto b (config.cbegin ()), i (b), e (config.cend ()); i != e; ++i)
      {
        if (i != b)
          v += ' ';

        v += *i;
      }

      s.next ("config", v);
    }

    s.next ("", ""); // End of manifest.
  }

  strings task_manifest::
  unquoted_config ()
  {
    return string_parser::unquote (config);
  }

  void task_manifest::
  check_config (const string& s)
  {
    auto i (s.begin ());
    auto e (s.end ());

    // Iterate until the variable name end and check that it contains no
    // whitespaces.
    //
    for (; i != e; ++i)
    {
      char c (*i);

      if (c == ' ' || c == '\t') // Whitespace in name.
        throw invalid_argument ("expected variable assignment");
      else if (c == '=')
        break;
    }

    if (i == e)
      throw invalid_argument ("no variable value");
  }

  // task_response_manifest
  //
  task_response_manifest::
  task_response_manifest (parser& p, bool iu)
  {
    name_value nv (p.next ());

    auto bad_name = [&p, &nv] (const string& d)
    {
      throw parsing (p.name (), nv.name_line, nv.name_column, d);
    };

    auto bad_value = [&p, &nv] (const string& d)
    {
      throw parsing (p.name (), nv.value_line, nv.value_column, d);
    };

    // Make sure this is the start and we support the version.
    //
    if (!nv.name.empty ())
      bad_name ("start of task response manifest expected");

    if (nv.value != "1")
      bad_value ("unsupported format version");

    // Parse the task response manifest.
    //
    // Note that we need to distinguish an empty and absent session.
    //
    optional<string> sess;
    for (nv = p.next (); !nv.empty (); nv = p.next ())
    {
      string& n (nv.name);
      string& v (nv.value);

      if (n == "session")
      {
        if (sess)
          bad_name ("task response session redefinition");

        sess = move (v);
      }
      else if (n == "challenge")
      {
        if (challenge)
          bad_name ("task response challenge redefinition");

        if (v.empty ())
          bad_value ("empty task response challenge");

        challenge = move (v);
      }
      else if (n == "result-url")
      {
        if (result_url)
          bad_name ("task response result url redefinition");

        if (v.empty ())
          bad_value ("empty task response result url");

        result_url = move (v);
      }
      else if (!iu)
        bad_name ("unknown name '" + n + "' in task response manifest");
    }

    // Verify all non-optional values were specified, and all values are
    // expected.
    //
    if (!sess)
      bad_value ("no task response session specified");

    session = move (*sess);

    // If session is not empty then the challenge may, and the result url
    // must, be present, otherwise they shouldn't.
    //
    if (!session.empty ())
    {
      if (!result_url)
        bad_value ("no task response result url specified");
    }
    else
    {
      if (challenge)
        bad_value ("unexpected task response challenge");

      if (result_url)
        bad_value ("unexpected task response result url");
    }

    // If session is not empty then the task manifest must follow, otherwise it
    // shouldn't.
    //
    nv = p.next ();

    if (!session.empty ())
    {
      if (nv.empty ())
        bad_value ("task manifest expected");

      task = task_manifest (p, nv, iu);

      nv = p.next ();
    }

    // Make sure this is the end.
    //
    if (!nv.empty ())
      throw parsing (p.name (), nv.name_line, nv.name_column,
                     "single task response manifest expected");
  }

  void task_response_manifest::
  serialize (serializer& s) const
  {
    // @@ Should we check that all non-optional values are specified and all
    //    values are valid?
    //
    s.next ("", "1"); // Start of manifest.
    s.next ("session", session);

    if (challenge)
      s.next ("challenge", *challenge);

    if (result_url)
      s.next ("result-url", *result_url);

    s.next ("", ""); // End of manifest.

    if (task)
      task->serialize (s);

    s.next ("", ""); // End of stream.
  }

  // result_manifest
  //
  result_manifest::
  result_manifest (parser& p, bool iu)
      : result_manifest (p, p.next (), iu)
  {
    // Make sure this is the end.
    //
    name_value nv (p.next ());
    if (!nv.empty ())
      throw parsing (p.name (), nv.name_line, nv.name_column,
                     "single result manifest expected");
  }

  result_manifest::
  result_manifest (parser& p, name_value nv, bool iu)
  {
    auto bad_name = [&p, &nv] (const string& d)
    {
      throw parsing (p.name (), nv.name_line, nv.name_column, d);
    };

    auto bad_value = [&p, &nv] (const string& d, size_t offset = 0)
    {
      throw parsing (p.name (), nv.value_line, nv.value_column + offset, d);
    };

    auto result_stat =
      [&bad_value] (const string& v, const string& what) -> result_status
    {
      try
      {
        return to_result_status (v);
      }
      catch (const invalid_argument&)
      {
        bad_value ("invalid " + what);
      }

      // Can't be here. Would be redundant if it were possible to declare
      // lambda with the [[noreturn]] attribute. Note that GCC (non-portably)
      // supports that.
      //
      return result_status::abnormal;
    };

    // Make sure this is the start and we support the version.
    //
    if (!nv.name.empty ())
      bad_name ("start of result manifest expected");

    if (nv.value != "1")
      bad_value ("unsupported format version");

    // Parse the result manifest.
    //
    optional<result_status> stat;

    // Number of parsed *-log values. Also denotes the next expected log type.
    //
    size_t nlog (0);

    for (nv = p.next (); !nv.empty (); nv = p.next ())
    {
      string& n (nv.name);
      string& v (nv.value);

      if (n == "name")
      {
        if (!name.empty ())
          bad_name ("result package name redefinition");

        if (v.empty ())
          bad_value ("empty result package name");

        name = move (v);
      }
      else if (n == "version")
      {
        if (!version.empty ())
          bad_name ("result package version redefinition");

        try
        {
          version = bpkg::version (move (v));
        }
        catch (const invalid_argument& e)
        {
          bad_value (string ("invalid result package version: ") + e.what ());
        }

        // Versions like 1.2.3- are forbidden in manifest as intended to be
        // used for version constrains rather than actual releases.
        //
        if (version.release && version.release->empty ())
          bad_value ("invalid result package version release");
      }
      else if (n == "status")
      {
        if (stat)
          bad_name ("result status redefinition");

        stat = result_stat (v, "result status");
      }
      else
      {
        size_t nn (n.size ()); // Name length.

        // Note: returns false if nothing preceeds a suffix.
        //
        auto suffix = [&n, nn] (const char* s, size_t sn) -> bool
        {
          return nn > sn && n.compare (nn - sn, sn, s) == 0;
        };

        size_t sn;
        if (suffix ("-status", sn = 7))
        {
          if (!stat)
            bad_name ("result status must appear first");

          if (nlog > 0) // Some logs have already been parsed.
            bad_name (n + " after operations logs");

          string op (n, 0, nn - sn);

          // Make sure the operation result status is not redefined.
          //
          for (const auto& r: results)
          {
            if (r.operation == op)
              bad_name ("result " + n + " redefinition");
          }

          // Add the operation result (log will come later).
          //
          results.push_back ({move (op), result_stat (v, n), string ()});
        }
        else if (suffix ("-log", sn = 4))
        {
          string op (n, 0, nn - sn);

          // Check that specifically this operation log is expected.
          //
          if (nlog >= results.size ())
            bad_name ("unexpected " + n);

          if (results[nlog].operation != op)
            bad_name (results[nlog].operation + "-log is expected");

          // Save operation log.
          //
          results[nlog++].log = move (v);
        }
        else if (!iu)
          bad_name ("unknown name '" + n + "' in result manifest");
      }
    }

    // Verify all non-optional values were specified.
    //
    if (name.empty ())
      bad_value ("no result package name specified");

    if (version.empty ())
      bad_value ("no result package version specified");

    if (!stat)
      bad_value ("no result status specified");

    // @@ Checking that the result status is consistent with operations
    //    statuses is a bit hairy, so let's postpone for now.
    //
    status = move (*stat);

    // Check that we have log for every operation result status.
    //
    if (nlog < results.size ())
      bad_name ("no result " + results[nlog].operation + "-log specified");
  }

  void result_manifest::
  serialize (serializer& s) const
  {
    // @@ Should we check that all non-optional values are specified and all
    //    values are valid?
    //
    s.next ("", "1"); // Start of manifest.
    s.next ("name", name);
    s.next ("version", version.string ());
    s.next ("status", to_string (status));

    // Serialize *-status values.
    //
    for (const auto& r: results)
      s.next (r.operation + "-status", to_string (r.status));

    // Serialize *-log values.
    //
    for (const auto& r: results)
      s.next (r.operation + "-log", r.log);

    s.next ("", ""); // End of manifest.
  }

  // result_request_manifest
  //
  result_request_manifest::
  result_request_manifest (parser& p, bool iu)
  {
    name_value nv (p.next ());

    auto bad_name = [&p, &nv] (const string& d)
    {
      throw parsing (p.name (), nv.name_line, nv.name_column, d);
    };

    auto bad_value = [&p, &nv] (const string& d)
    {
      throw parsing (p.name (), nv.value_line, nv.value_column, d);
    };

    // Make sure this is the start and we support the version.
    //
    if (!nv.name.empty ())
      bad_name ("start of result request manifest expected");

    if (nv.value != "1")
      bad_value ("unsupported format version");

    // Parse the result request manifest.
    //
    for (nv = p.next (); !nv.empty (); nv = p.next ())
    {
      string& n (nv.name);
      string& v (nv.value);

      if (n == "session")
      {
        if (!session.empty ())
          bad_name ("result request session redefinition");

        if (v.empty ())
          bad_value ("empty result request session");

        session = move (v);
      }
      else if (n == "challenge")
      {
        if (challenge)
          bad_name ("result request challenge redefinition");

        if (v.empty ())
          bad_value ("empty result request challenge");

        challenge = move (v);
      }
      else if (!iu)
        bad_name ("unknown name '" + n + "' in result request manifest");
    }

    // Verify all non-optional values were specified.
    //
    if (session.empty ())
      bad_value ("no result request session specified");

    nv = p.next ();
    if (nv.empty ())
      bad_value ("result manifest expected");

    result = result_manifest (p, nv, iu);

    // Make sure this is the end.
    //
    nv = p.next ();
    if (!nv.empty ())
      throw parsing (p.name (), nv.name_line, nv.name_column,
                     "single result request manifest expected");
  }

  void result_request_manifest::
  serialize (serializer& s) const
  {
    // @@ Should we check that all non-optional values are specified and all
    //    values are valid?
    //
    s.next ("", "1"); // Start of manifest.
    s.next ("session", session);

    if (challenge)
      s.next ("challenge", *challenge);

    s.next ("", ""); // End of manifest.

    result.serialize (s);
    s.next ("", ""); // End of stream.
  }
}