summaryrefslogtreecommitdiff
path: root/src/backend/taler-merchant-httpd_private-get-orders.c
blob: 5b1ab93428970adbbccfccbfbe08b1c575965821 (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
/*
  This file is part of TALER
  (C) 2019-2021 Taler Systems SA

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

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

  You should have received a copy of the GNU General Public License along with
  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file taler-merchant-httpd_private-get-orders.c
 * @brief implement GET /orders
 * @author Christian Grothoff
 */
#include "platform.h"
#include "taler-merchant-httpd_private-get-orders.h"
#include <taler/taler_json_lib.h>


/**
 * Sensible bound on TALER_MERCHANTDB_OrderFilter.delta
 */
#define MAX_DELTA 1024


/**
 * A pending GET /orders request.
 */
struct TMH_PendingOrder
{

  /**
   * Kept in a DLL.
   */
  struct TMH_PendingOrder *prev;

  /**
   * Kept in a DLL.
   */
  struct TMH_PendingOrder *next;

  /**
   * Which connection was suspended.
   */
  struct MHD_Connection *con;

  /**
   * Associated heap node.
   */
  struct GNUNET_CONTAINER_HeapNode *hn;

  /**
   * Which instance is this client polling? This also defines
   * which DLL this struct is part of.
   */
  struct TMH_MerchantInstance *mi;

  /**
   * At what time does this request expire? If set in the future, we
   * may wait this long for a payment to arrive before responding.
   */
  struct GNUNET_TIME_Absolute long_poll_timeout;

  /**
   * Filter to apply.
   */
  struct TALER_MERCHANTDB_OrderFilter of;

  /**
   * The array of orders.
   */
  json_t *pa;

  /**
   * The name of the instance we are querying for.
   */
  const char *instance_id;

  /**
   * The result after adding the orders (#TALER_EC_NONE for okay, anything else for an error).
   */
  enum TALER_ErrorCode result;

  /**
   * Is the structure in the DLL
   */
  bool in_dll;
};


/**
 * Task to timeout pending orders.
 */
static struct GNUNET_SCHEDULER_Task *order_timeout_task;

/**
 * Heap for orders in long polling awaiting timeout.
 */
static struct GNUNET_CONTAINER_Heap *order_timeout_heap;


/**
 * We are shutting down (or an instance is being deleted), force resume of all
 * GET /orders requests.
 *
 * @param mi instance to force resuming for
 */
void
TMH_force_get_orders_resume (struct TMH_MerchantInstance *mi)
{
  struct TMH_PendingOrder *po;

  while (NULL != (po = mi->po_head))
  {
    GNUNET_assert (po->in_dll);
    GNUNET_CONTAINER_DLL_remove (mi->po_head,
                                 mi->po_tail,
                                 po);
    GNUNET_assert (po ==
                   GNUNET_CONTAINER_heap_remove_root (order_timeout_heap));
    MHD_resume_connection (po->con);
    po->in_dll = false;
  }
  if (NULL != order_timeout_task)
  {
    GNUNET_SCHEDULER_cancel (order_timeout_task);
    order_timeout_task = NULL;
  }
  if (NULL != order_timeout_heap)
  {
    GNUNET_CONTAINER_heap_destroy (order_timeout_heap);
    order_timeout_heap = NULL;
  }
}


/**
 * Task run to trigger timeouts on GET /orders requests with long polling.
 *
 * @param cls unused
 */
static void
order_timeout (void *cls)
{
  struct TMH_PendingOrder *po;
  struct TMH_MerchantInstance *mi;

  (void) cls;
  order_timeout_task = NULL;
  while (1)
  {
    po = GNUNET_CONTAINER_heap_peek (order_timeout_heap);
    if (NULL == po)
    {
      /* release data structure, we don't need it right now */
      GNUNET_CONTAINER_heap_destroy (order_timeout_heap);
      order_timeout_heap = NULL;
      return;
    }
    if  (0 !=
         GNUNET_TIME_absolute_get_remaining (
           po->long_poll_timeout).rel_value_us)
      break;
    GNUNET_assert (po ==
                   GNUNET_CONTAINER_heap_remove_root (order_timeout_heap));
    po->hn = NULL;
    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                "Resuming long polled job due to timeout\n");
    mi = po->mi;
    GNUNET_assert (po->in_dll);
    GNUNET_CONTAINER_DLL_remove (mi->po_head,
                                 mi->po_tail,
                                 po);
    po->in_dll = false;
    MHD_resume_connection (po->con);
    TMH_trigger_daemon ();   /* we resumed, kick MHD */
  }
  order_timeout_task = GNUNET_SCHEDULER_add_at (po->long_poll_timeout,
                                                &order_timeout,
                                                NULL);
}


/**
 * Cleanup our "context", where we stored the JSON array
 * we are building for the response.
 *
 * @param ctx context to clean up, must be a `struct AddOrderState *`
 */
static void
cleanup (void *ctx)
{
  struct TMH_PendingOrder *po = ctx;

  if (po->in_dll)
  {
    struct TMH_MerchantInstance *mi = po->mi;

    GNUNET_CONTAINER_DLL_remove (mi->po_head,
                                 mi->po_tail,
                                 po);
  }
  if (NULL != po->hn)
    GNUNET_assert (po ==
                   GNUNET_CONTAINER_heap_remove_node (po->hn));
  json_decref (po->pa);
  GNUNET_free (po);
}


/**
 * Function called with information about a refund.
 * It is responsible for summing up the refund amount.
 *
 * @param cls closure
 * @param refund_serial unique serial number of the refund
 * @param timestamp time of the refund (for grouping of refunds in the wallet UI)
 * @param coin_pub public coin from which the refund comes from
 * @param exchange_url URL of the exchange that issued @a coin_pub
 * @param rtransaction_id identificator of the refund
 * @param reason human-readable explanation of the refund
 * @param refund_amount refund amount which is being taken from @a coin_pub
 * @param pending true if the this refund was not yet processed by the wallet/exchange
 */
static void
process_refunds_cb (void *cls,
                    uint64_t refund_serial,
                    struct GNUNET_TIME_Absolute timestamp,
                    const struct TALER_CoinSpendPublicKeyP *coin_pub,
                    const char *exchange_url,
                    uint64_t rtransaction_id,
                    const char *reason,
                    const struct TALER_Amount *refund_amount,
                    bool pending)
{
  struct TALER_Amount *total_refund_amount = cls;

  GNUNET_assert (0 <=
                 TALER_amount_add (total_refund_amount,
                                   total_refund_amount,
                                   refund_amount));
}


/**
 * Add order details to our JSON array.
 *
 * @param[in,out] cls a `json_t *` JSON array to build
 * @param order_id ID of the order
 * @param order_serial serial ID of the order
 * @param creation_time when was the order created
 */
static void
add_order (void *cls,
           const char *order_id,
           uint64_t order_serial,
           struct GNUNET_TIME_Absolute creation_time)
{
  struct TMH_PendingOrder *po = cls;
  json_t *contract_terms;
  struct GNUNET_HashCode h_contract_terms;
  enum GNUNET_DB_QueryStatus qs;
  const char *summary;
  bool refundable = false;
  bool paid;
  struct TALER_Amount order_amount;

  qs = TMH_db->lookup_order_status (TMH_db->cls,
                                    po->instance_id,
                                    order_id,
                                    &h_contract_terms,
                                    &paid);
  /* qs == 0: contract terms don't exist, so the order cannot be paid. */
  if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
    paid = false;
  if (qs < 0)
  {
    GNUNET_break (0);
    po->result = TALER_EC_GENERIC_DB_FETCH_FAILED;
    return;
  }

  if (paid)
  {
    /* if the order was paid, it must have been claimed, so use
       lookup_contract_terms to avoid the order being deleted in the db. */
    uint64_t os;

    qs = TMH_db->lookup_contract_terms (TMH_db->cls,
                                        po->instance_id,
                                        order_id,
                                        &contract_terms,
                                        &os);
  }
  else
  {
    struct GNUNET_HashCode unused;

    qs = TMH_db->lookup_order (TMH_db->cls,
                               po->instance_id,
                               order_id,
                               NULL,
                               &unused,
                               &contract_terms);
  }

  if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs)
  {
    GNUNET_break (0);
    po->result = TALER_EC_GENERIC_DB_FETCH_FAILED;
    json_decref (contract_terms);
    return;
  }

  {
    struct GNUNET_TIME_Absolute rd;
    struct GNUNET_JSON_Specification spec[] = {
      TALER_JSON_spec_amount ("amount",
                              TMH_currency,
                              &order_amount),
      GNUNET_JSON_spec_absolute_time ("refund_deadline",
                                      &rd),
      GNUNET_JSON_spec_string ("summary",
                               &summary),
      GNUNET_JSON_spec_end ()
    };

    if (GNUNET_OK !=
        GNUNET_JSON_parse (contract_terms,
                           spec,
                           NULL, NULL))
    {
      GNUNET_break (0);
      po->result = TALER_EC_MERCHANT_GENERIC_DB_CONTRACT_CONTENT_INVALID;
      json_decref (contract_terms);
      return;
    }

    if (GNUNET_TIME_absolute_is_future (rd) &&
        paid)
    {
      struct TALER_Amount refund_amount;

      GNUNET_assert (GNUNET_OK ==
                     TALER_amount_set_zero (TMH_currency,
                                            &refund_amount));
      qs = TMH_db->lookup_refunds_detailed (TMH_db->cls,
                                            po->instance_id,
                                            &h_contract_terms,
                                            &process_refunds_cb,
                                            &refund_amount);
      if (0 > qs)
      {
        GNUNET_break (0);
        po->result = TALER_EC_GENERIC_DB_FETCH_FAILED;
        json_decref (contract_terms);
        return;
      }
      if (0 > TALER_amount_cmp (&refund_amount,
                                &order_amount))
        refundable = true;
    }
  }

  GNUNET_assert (0 ==
                 json_array_append_new (
                   po->pa,
                   GNUNET_JSON_PACK (
                     GNUNET_JSON_pack_string ("order_id",
                                              order_id),
                     GNUNET_JSON_pack_uint64 ("row_id",
                                              order_serial),
                     GNUNET_JSON_pack_time_abs ("timestamp",
                                                creation_time),
                     TALER_JSON_pack_amount ("amount",
                                             &order_amount),
                     GNUNET_JSON_pack_string ("summary",
                                              summary),
                     GNUNET_JSON_pack_bool ("refundable",
                                            refundable),
                     GNUNET_JSON_pack_bool ("paid",
                                            paid))));
  json_decref (contract_terms);
}


/**
 * There has been a change or addition of a new @a order_id.  Wake up
 * long-polling clients that may have been waiting for this event.
 *
 * @param mi the instance where the order changed
 * @param order_id the order that changed
 * @param paid is the order paid by the customer?
 * @param refunded was the order refunded?
 * @param wired was the merchant paid via wire transfer?
 * @param date execution date of the order
 * @param order_serial_id serial ID of the order in the database
 */
void
TMH_notify_order_change (struct TMH_MerchantInstance *mi,
                         const char *order_id,
                         bool paid,
                         bool refunded,
                         bool wired,
                         struct GNUNET_TIME_Absolute date,
                         uint64_t order_serial_id)
{
  struct TMH_PendingOrder *pn;

  for (struct TMH_PendingOrder *po = mi->po_head;
       NULL != po;
       po = pn)
  {
    pn = po->next;
    if (! ( ( ((TALER_EXCHANGE_YNA_YES == po->of.paid) == paid) ||
              (TALER_EXCHANGE_YNA_ALL == po->of.paid) ) &&
            ( ((TALER_EXCHANGE_YNA_YES == po->of.refunded) == refunded) ||
              (TALER_EXCHANGE_YNA_ALL == po->of.refunded) ) &&
            ( ((TALER_EXCHANGE_YNA_YES == po->of.wired) == wired) ||
              (TALER_EXCHANGE_YNA_ALL == po->of.wired) ) ) )
      continue;
    if (po->of.delta > 0)
    {
      if (order_serial_id < po->of.start_row)
        continue;
      if (date.abs_value_us < po->of.date.abs_value_us)
        continue;
      po->of.delta--;
    }
    else
    {
      if (order_serial_id > po->of.start_row)
        continue;
      if (date.abs_value_us > po->of.date.abs_value_us)
        continue;
      po->of.delta++;
    }
    add_order (po,
               order_id,
               order_serial_id,
               date);
    GNUNET_assert (po->in_dll);
    GNUNET_CONTAINER_DLL_remove (mi->po_head,
                                 mi->po_tail,
                                 po);
    po->in_dll = false;
    GNUNET_assert (po ==
                   GNUNET_CONTAINER_heap_remove_node (po->hn));
    MHD_resume_connection (po->con);
    TMH_trigger_daemon ();   /* we resumed, kick MHD */
  }
}


/**
 * Handle a GET "/orders" request.
 *
 * @param rh context of the handler
 * @param connection the MHD connection to handle
 * @param[in,out] hc context with further information about the request
 * @return MHD result code
 */
MHD_RESULT
TMH_private_get_orders (const struct TMH_RequestHandler *rh,
                        struct MHD_Connection *connection,
                        struct TMH_HandlerContext *hc)
{
  struct TMH_PendingOrder *po = hc->ctx;
  enum GNUNET_DB_QueryStatus qs;
  struct TALER_MERCHANTDB_OrderFilter of;

  if (NULL != po)
  {
    /* resumed from long-polling, return answer we already have
       in 'hc->ctx' */
    if (TALER_EC_NONE != po->result)
    {
      GNUNET_break (0);
      return TALER_MHD_reply_with_error (connection,
                                         MHD_HTTP_INTERNAL_SERVER_ERROR,
                                         po->result,
                                         NULL);
    }
    return TALER_MHD_REPLY_JSON_PACK (
      connection,
      MHD_HTTP_OK,
      GNUNET_JSON_pack_array_incref ("orders",
                                     po->pa));
  }

  if (! (TALER_arg_to_yna (connection,
                           "paid",
                           TALER_EXCHANGE_YNA_ALL,
                           &of.paid)) )
    return TALER_MHD_reply_with_error (connection,
                                       MHD_HTTP_BAD_REQUEST,
                                       TALER_EC_GENERIC_PARAMETER_MALFORMED,
                                       "paid");
  if (! (TALER_arg_to_yna (connection,
                           "refunded",
                           TALER_EXCHANGE_YNA_ALL,
                           &of.refunded)) )
    return TALER_MHD_reply_with_error (connection,
                                       MHD_HTTP_BAD_REQUEST,
                                       TALER_EC_GENERIC_PARAMETER_MALFORMED,
                                       "refunded");
  if (! (TALER_arg_to_yna (connection,
                           "wired",
                           TALER_EXCHANGE_YNA_ALL,
                           &of.wired)) )
    return TALER_MHD_reply_with_error (connection,
                                       MHD_HTTP_BAD_REQUEST,
                                       TALER_EC_GENERIC_PARAMETER_MALFORMED,
                                       "wired");
  {
    const char *delta_str;

    delta_str = MHD_lookup_connection_value (connection,
                                             MHD_GET_ARGUMENT_KIND,
                                             "delta");
    if (NULL == delta_str)
    {
      of.delta = -20;
    }
    else
    {
      char dummy;
      long long ll;

      if (1 !=
          sscanf (delta_str,
                  "%lld%c",
                  &ll,
                  &dummy))
      {
        GNUNET_break_op (0);
        return TALER_MHD_reply_with_error (connection,
                                           MHD_HTTP_BAD_REQUEST,
                                           TALER_EC_GENERIC_PARAMETER_MALFORMED,
                                           "delta");
      }
      of.delta = (int64_t) ll;
      if ( (-MAX_DELTA > of.delta) ||
           (of.delta > MAX_DELTA) )
      {
        GNUNET_break_op (0);
        return TALER_MHD_reply_with_error (connection,
                                           MHD_HTTP_BAD_REQUEST,
                                           TALER_EC_GENERIC_PARAMETER_MALFORMED,
                                           "delta");
      }
    }
  }
  {
    const char *date_ms_str;

    date_ms_str = MHD_lookup_connection_value (connection,
                                               MHD_GET_ARGUMENT_KIND,
                                               "date_ms");
    if (NULL == date_ms_str)
    {
      if (of.delta > 0)
        of.date = GNUNET_TIME_UNIT_ZERO_ABS;
      else
        of.date = GNUNET_TIME_UNIT_FOREVER_ABS;
    }
    else
    {
      char dummy;
      unsigned long long ll;

      if (1 !=
          sscanf (date_ms_str,
                  "%llu%c",
                  &ll,
                  &dummy))
      {
        GNUNET_break_op (0);
        return TALER_MHD_reply_with_error (connection,
                                           MHD_HTTP_BAD_REQUEST,
                                           TALER_EC_GENERIC_PARAMETER_MALFORMED,
                                           "date_ms");
      }

      of.date = GNUNET_TIME_absolute_from_ms (ll);
      if (GNUNET_TIME_absolute_is_never (of.date))
      {
        GNUNET_break_op (0);
        return TALER_MHD_reply_with_error (connection,
                                           MHD_HTTP_BAD_REQUEST,
                                           TALER_EC_GENERIC_PARAMETER_MALFORMED,
                                           "date_ms");
      }
    }
  }
  {
    const char *start_row_str;

    start_row_str = MHD_lookup_connection_value (connection,
                                                 MHD_GET_ARGUMENT_KIND,
                                                 "start");
    if (NULL == start_row_str)
    {
      if (of.delta > 0)
        of.start_row = 0;
      else
        of.start_row = INT64_MAX;
    }
    else
    {
      char dummy;
      unsigned long long ull;

      if (1 !=
          sscanf (start_row_str,
                  "%llu%c",
                  &ull,
                  &dummy))
        return TALER_MHD_reply_with_error (connection,
                                           MHD_HTTP_BAD_REQUEST,
                                           TALER_EC_GENERIC_PARAMETER_MALFORMED,
                                           "start");
      of.start_row = (uint64_t) ull;
      if (INT64_MAX < of.start_row)
      {
        GNUNET_break_op (0);
        return TALER_MHD_reply_with_error (connection,
                                           MHD_HTTP_BAD_REQUEST,
                                           TALER_EC_GENERIC_PARAMETER_MALFORMED,
                                           "start");
      }
    }
  }
  {
    const char *timeout_ms_str;

    timeout_ms_str = MHD_lookup_connection_value (connection,
                                                  MHD_GET_ARGUMENT_KIND,
                                                  "timeout_ms");
    if (NULL == timeout_ms_str)
    {
      of.timeout = GNUNET_TIME_UNIT_ZERO;
    }
    else
    {
      char dummy;
      unsigned long long ull;

      if (1 !=
          sscanf (timeout_ms_str,
                  "%lld%c",
                  &ull,
                  &dummy))
        return TALER_MHD_reply_with_error (connection,
                                           MHD_HTTP_BAD_REQUEST,
                                           TALER_EC_GENERIC_PARAMETER_MALFORMED,
                                           "timeout_ms");
      of.timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
                                                  ull);
      if (GNUNET_TIME_relative_is_forever (of.timeout))
      {
        GNUNET_break_op (0);
        return TALER_MHD_reply_with_error (connection,
                                           MHD_HTTP_BAD_REQUEST,
                                           TALER_EC_GENERIC_PARAMETER_MALFORMED,
                                           "timeout_ms");
      }
    }

    if ( (0 >= of.delta) &&
         (! GNUNET_TIME_relative_is_zero (of.timeout)) )
    {
      GNUNET_break_op (0);
      of.timeout = GNUNET_TIME_UNIT_ZERO;
    }
  }

  po = GNUNET_new (struct TMH_PendingOrder);
  hc->ctx = po;
  hc->cc = &cleanup;
  po->con = connection;
  po->pa = json_array ();
  GNUNET_assert (NULL != po->pa);
  po->instance_id = hc->instance->settings.id;
  po->mi = hc->instance;

  qs = TMH_db->lookup_orders (TMH_db->cls,
                              po->instance_id,
                              &of,
                              &add_order,
                              po);
  if (0 > qs)
  {
    po->result = TALER_EC_GENERIC_DB_FETCH_FAILED;
  }
  if (TALER_EC_NONE != po->result)
  {
    GNUNET_break (0);
    return TALER_MHD_reply_with_error (connection,
                                       MHD_HTTP_INTERNAL_SERVER_ERROR,
                                       po->result,
                                       NULL);
  }
  if ( (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) &&
       (! GNUNET_TIME_relative_is_zero (of.timeout)) )
  {
    struct TMH_MerchantInstance *mi = hc->instance;

    /* setup timeout heap (if not yet exists) */
    if (NULL == order_timeout_heap)
      order_timeout_heap
        = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
    po->hn = GNUNET_CONTAINER_heap_insert (order_timeout_heap,
                                           po,
                                           po->long_poll_timeout.abs_value_us);
    po->long_poll_timeout = GNUNET_TIME_relative_to_absolute (of.timeout);
    po->of = of;
    GNUNET_CONTAINER_DLL_insert (mi->po_head,
                                 mi->po_tail,
                                 po);
    po->in_dll = true;
    MHD_suspend_connection (connection);
    {
      struct TMH_PendingOrder *pot;

      /* start timeout task */
      pot = GNUNET_CONTAINER_heap_peek (order_timeout_heap);
      if (NULL != order_timeout_task)
        GNUNET_SCHEDULER_cancel (order_timeout_task);
      order_timeout_task = GNUNET_SCHEDULER_add_at (pot->long_poll_timeout,
                                                    &order_timeout,
                                                    NULL);
    }
    return MHD_YES;
  }
  return TALER_MHD_REPLY_JSON_PACK (
    connection,
    MHD_HTTP_OK,
    GNUNET_JSON_pack_array_incref ("orders",
                                   po->pa));
}


/* end of taler-merchant-httpd_private-get-orders.c */