GNU libmicrohttpd  0.9.72
response_from_buffer.c
Go to the documentation of this file.
1 /*
2  This file is part of libmicrohttpd
3  Copyright (C) 2007-2018 Daniel Pittman and Christian Grothoff
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
25 #include "internal.h"
26 
27 
40 struct MHD_Response *
41 MHD_response_from_buffer (enum MHD_HTTP_StatusCode sc,
42  size_t size,
43  void *buffer,
44  enum MHD_ResponseMemoryMode mode)
45 {
46  struct MHD_Response *response;
47  void *tmp;
48 
49  mhd_assert ( (NULL != buffer) ||
50  (0 == size) );
51  if (NULL ==
52  (response = MHD_calloc_ (1,
53  sizeof (struct MHD_Response))))
54  return NULL;
55  response->fd = -1;
56  if (! MHD_mutex_init_ (&response->mutex))
57  {
58  free (response);
59  return NULL;
60  }
61  if ( (MHD_RESPMEM_MUST_COPY == mode) &&
62  (size > 0) )
63  {
64  if (NULL == (tmp = malloc (size)))
65  {
66  MHD_mutex_destroy_chk_ (&response->mutex);
67  free (response);
68  return NULL;
69  }
70  memcpy (tmp,
71  buffer,
72  size);
73  buffer = tmp;
74  }
75  if (MHD_RESPMEM_PERSISTENT != mode)
76  {
77  response->crfc = &free;
78  response->crc_cls = buffer;
79  }
80  response->status_code = sc;
81  response->reference_count = 1;
82  response->total_size = size;
83  response->data = buffer;
84  response->data_size = size;
85  return response;
86 }
87 
88 
89 /* end of response_from_buffer.c */
MHD_ResponseMemoryMode
Definition: microhttpd.h:3064
struct MHD_Response * MHD_response_from_buffer(enum MHD_HTTP_StatusCode sc, size_t size, void *buffer, enum MHD_ResponseMemoryMode mode)
@ MHD_RESPMEM_PERSISTENT
Definition: microhttpd.h:3072
@ MHD_RESPMEM_MUST_COPY
Definition: microhttpd.h:3089
#define mhd_assert(CHK)
Definition: mhd_assert.h:39
void * MHD_calloc_(size_t nelem, size_t elsize)
Definition: mhd_compat.c:98
#define MHD_mutex_destroy_chk_(pmutex)
Definition: mhd_locks.h:121
#define NULL
Definition: reason_phrase.c:30
internal shared structures
#define MHD_mutex_init_(ignore)
Definition: mhd_locks.h:189
MHD_ContentReaderFreeCallback crfc
Definition: internal.h:1606
void * crc_cls
Definition: internal.h:1594
enum MHD_HTTP_StatusCode status_code
Definition: internal.h:1669
size_t data_size
Definition: internal.h:1659
unsigned int reference_count
Definition: internal.h:1675
char * data
Definition: internal.h:1588
MHD_mutex_ mutex
Definition: internal.h:1637
uint64_t total_size
Definition: internal.h:1642