[libmicrohttpd] GNU libmicrohttpd 0.9.63 released

2019-02-10 Thread Christian Grothoff
Dear all,

I'm glad to announce the release of GNU libmicrohttpd 0.9.63.

GNU libmicrohttpd is a small C library that is supposed to make it easy
to run an HTTP server as part of another application. GNU libmicrohttpd
is fully HTTP 1.1 compliant and supports IPv6. Finally, GNU
libmicrohttpd is fast, portable and has a simple API and (without TLS
support and other optional features) a small binary size (~32k).

This is bugfix release.  Most noticeable changes since version 0.9.62:

* Fixed regression with respect to the URI log callback and URI
  escaping, log callback should again receive the raw arguments
  without prior escaping
* Added minimal example for how to compress HTTP responses
* Fix in timeout handling in thread-per-connection mode
* Fix possible block on W32 when using threadpool
* Avoid VLA arrays with compilers that do not support them

You can download GNU libmicrohttpd from

* https://ftp.gnu.org/gnu/libmicrohttpd/ and all GNU FTP mirrors.
* Our git repository at https://gnunet.org/git/libmicrohttpd.git

Please report bugs to our bugtracker at https://gnunet.org/bugs/.

The documentation (including a reference manual and tutorial) can be
found at https://www.gnu.org/software/libmicrohttpd/.

Happy hacking!

Christian



signature.asc
Description: OpenPGP digital signature


Re: [libmicrohttpd] MHD cannot write the entire chunked deflate data (deflate/chunked)

2019-02-10 Thread silvioprog
Hello Christian,

first of all, thanks a lot for answering, specially for the info regarding
compress2(). After your message I decided to study this function internally
and now I know its entire logic.

I've read the zlib manual and took a look at its examples/tests to get a
better knowledge about deflate. And more, I found a logic in the MHD commit
history which helped me a lot to understand it in practice. Lastly, I
managed to solve the problem, and I wrote a tiny example for how compress
data using the deflate algorithm and, if you agree, I could put the credits
and the MHD licence on it and distribute it as example in the MHD sources.

This is the example (now it contains some error handling). Feel absolutely
free to change it if you find something that could be improved, and let me
know if it could be useful and distributed in the "src/examples/":

#include 
#include 
#include 
#include 
#include 
#include 

#define CHUNK 16384

struct Holder {
FILE *file;
z_stream stream;
void *buf;
};

static int
compressMem (z_stream *strm, const void *src, size_t src_size, size_t
*offset, void **dest, size_t *dest_size,
 void *tmp)
{
  unsigned int have;
  int ret;
  int flush;
  *dest = NULL;
  *dest_size = 0;
  do
{
  if (src_size > CHUNK)
{
  strm->avail_in = CHUNK;
  src_size -= CHUNK;
  flush = Z_NO_FLUSH;
}
  else
{
  strm->avail_in = (uInt) src_size;
  flush = Z_SYNC_FLUSH;
}
  *offset += strm->avail_in;
  strm->next_in = (Bytef *) src;
  do
{
  strm->avail_out = CHUNK;
  strm->next_out = tmp;
  ret = deflate (strm, flush);
  have = CHUNK - strm->avail_out;
  *dest_size += have;
  *dest = realloc (*dest, *dest_size);
  if (NULL == *dest)
return MHD_NO;
  memcpy ((*dest) + ((*dest_size) - have), tmp, have);
}
  while (0 == strm->avail_out);
}
  while (flush != Z_SYNC_FLUSH);
  return (Z_OK == ret) ? MHD_YES : MHD_NO;
}

static ssize_t
read_cb (void *cls, uint64_t pos, char *mem, size_t size)
{
  struct Holder *holder = cls;
  void *src;
  void *buf;
  src = malloc (size);
  if (NULL == src)
return MHD_CONTENT_READER_END_WITH_ERROR;
  size = fread (src, 1, size, holder->file);
  if (size < 0)
{
  size = MHD_CONTENT_READER_END_WITH_ERROR;
  goto done;
}
  if (0 == size)
{
  size = MHD_CONTENT_READER_END_OF_STREAM;
  goto done;
}
  if (MHD_YES != compressMem (&holder->stream, src, size, &pos, &buf,
&size, holder->buf))
size = MHD_CONTENT_READER_END_WITH_ERROR;
  else
{
  memcpy (mem, buf, size);
  free (buf);
}
done:
  free (src);
  return size;
}

static void
free_cb (void *cls)
{
  struct Holder *holder = cls;
  fclose (holder->file);
  deflateEnd (&holder->stream);
  free (holder->buf);
  free (holder);
}

static int
ahc_echo (void *cls, struct MHD_Connection *con, const char *url,
const char *method, const char *version,
  const char *upload_data, size_t *upload_size, void **ptr)
{
  struct Holder *holder;
  struct MHD_Response *res;
  int ret;
  (void) cls;
  (void) url;
  (void) method;
  (void) version;
  (void) upload_data;
  (void) upload_size;
  if (NULL == *ptr)
{
  *ptr = (void *) 1;
  return MHD_YES;
}
  *ptr = NULL;
  holder = calloc (1, sizeof (struct Holder));
  if (!holder)
return MHD_NO;
  holder->file = fopen (__FILE__, "rb");
  if (NULL == holder->file)
goto error;
  holder->buf = malloc (CHUNK);
  if (NULL == holder->buf)
goto error;
  ret = deflateInit(&holder->stream, Z_BEST_COMPRESSION);
  if (ret != Z_OK)
goto error;
  res = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 1024,
&read_cb, holder, &free_cb);
  if (NULL == res)
goto error;
  ret = MHD_add_response_header (res,
MHD_HTTP_HEADER_CONTENT_ENCODING, "deflate");
  if (MHD_YES != ret)
return MHD_NO;
  ret = MHD_add_response_header (res, MHD_HTTP_HEADER_CONTENT_TYPE, "text/x-c");
  if (MHD_YES != ret)
return MHD_NO;
  ret = MHD_queue_response (con, MHD_HTTP_OK, res);
  MHD_destroy_response (res);
  return ret;
error:
  fclose (holder->file);
  free (holder->buf);
  return MHD_NO;
}

int
main (int argc, char *const *argv)
{
  struct MHD_Daemon *d;
  unsigned int port;
  if ((argc != 2) ||
  (1 != sscanf (argv[1], "%u", &port)) ||
  (UINT16_MAX < port))
{
  fprintf (stderr,
   "%s PORT\n", argv[0]);
  return 1;
}
  d = MHD_start_daemon (MHD_USE_AUTO |
MHD_USE_INTERNAL_POLLING_THREAD, port, NULL, NULL,
&ahc_echo, NULL,
MHD_OPTION_END);
  if (NULL == d)
return 1;
  if (0 == port)
MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT, &port);
  fprintf (stdout, "HTTP server running at
http://localhost:%u\n\nPress ENTER to stop the server ...\n", port);
  (void) getc (stdin);
  MHD_stop_daemon (d);
  return 0;
}

best,

On S

Re: [libmicrohttpd] GNU libmicrohttpd 0.9.63 released

2019-02-10 Thread silvioprog
Great news! \o/

Thank to all for the effort to release this new version, I've tested it on
Windows and its build now works like a charm.

best,

On Sun, Feb 10, 2019 at 1:24 PM Christian Grothoff 
wrote:

> Dear all,
>
> I'm glad to announce the release of GNU libmicrohttpd 0.9.63.
>
> GNU libmicrohttpd is a small C library that is supposed to make it easy
> to run an HTTP server as part of another application. GNU libmicrohttpd
> is fully HTTP 1.1 compliant and supports IPv6. Finally, GNU
> libmicrohttpd is fast, portable and has a simple API and (without TLS
> support and other optional features) a small binary size (~32k).
>
> This is bugfix release.  Most noticeable changes since version 0.9.62:
>
> * Fixed regression with respect to the URI log callback and URI
>   escaping, log callback should again receive the raw arguments
>   without prior escaping
> * Added minimal example for how to compress HTTP responses
> * Fix in timeout handling in thread-per-connection mode
> * Fix possible block on W32 when using threadpool
> * Avoid VLA arrays with compilers that do not support them
>
> You can download GNU libmicrohttpd from
>
> * https://ftp.gnu.org/gnu/libmicrohttpd/ and all GNU FTP mirrors.
> * Our git repository at https://gnunet.org/git/libmicrohttpd.git
>
> Please report bugs to our bugtracker at https://gnunet.org/bugs/.
>
> The documentation (including a reference manual and tutorial) can be
> found at https://www.gnu.org/software/libmicrohttpd/.
>
> Happy hacking!
>
> Christian
>
>

-- 
Silvio Clécio


Re: [libmicrohttpd] MHD cannot write the entire chunked deflate data (deflate/chunked)

2019-02-10 Thread Christian Grothoff
On 2/10/19 10:45 PM, silvioprog wrote:
> 
> This is the example (now it contains some error handling). Feel
> absolutely free to change it if you find something that could be
> improved, and let me know if it could be useful and distributed in the
> "src/examples/":

Sure, except for one thing: your clean up is not clean: on the "error:"
path, you do not "free(holder)", and on some of the "return MHD_NO"
paths you leak the entire response object.

If you fix those minor nitpicks, this should make a nice example.

Thanks & happy hacking!

-Christian



signature.asc
Description: OpenPGP digital signature


Re: [libmicrohttpd] MHD cannot write the entire chunked deflate data (deflate/chunked)

2019-02-10 Thread silvioprog
On Sun, Feb 10, 2019 at 7:08 PM Christian Grothoff 
wrote:

> Sure, except for one thing: your clean up is not clean: on the "error:"
> path, you do not "free(holder)", and on some of the "return MHD_NO"
> paths you leak the entire response object.
>
> If you fix those minor nitpicks, this should make a nice example.
>
> Thanks & happy hacking!
>
> -Christian
>

Thanks for the code review! :-)

Indeed, it would generate memory leak. I'm going to fix it and send the
refactored example to the MHD trunk.

Thank you very much! (y)

-- 
Silvio Clécio


Re: [libmicrohttpd] tiny websocket above LMHD

2019-02-10 Thread silvioprog
Hello José, thanks for answering.

Sorry for resend, I don't know why the Gmail client sent it to your
particular e-mail. 🤔

So, I have a question. Could I send a feature-request at lmhd-ws/issues
?  If so, I'll open a new issue as
feature-request to build the library on Windows using the MSYS2 tools
http://www.msys2.org .

Thank you! (y)

On Tue, Feb 5, 2019 at 6:56 AM José Bollo  wrote:

> On Sun, 3 Feb 2019 22:35:13 -0300
> silvioprog  wrote:
>
> > Hello again José,
>
> Hi Silvio,
>
> > I tried the library on Linux and Windows. On Linux, it works, i.e.,
> > the client sends text to the server, the only problem I've found was
> > that Ctrl+C in the client closes the server too, but a server
> > shouldn't be closed by a client.
>
> Perhaps some "signal(SIGPIPE, SIG_IGN);" is missing
>
> > On Windows (latest mingw-w64
> > ), I couldn't compile it. I've got some errors
> > like "array type has incomplete element type 'struct iovec'",
> > "'F_SETFL' undeclared", "'O_NONBLOCK' undeclared", maybe by missing
> > symbols specific from Linux that aren't present on Windows. I've
> > tried to solve it, but it is a little bit hard to be solved, and
> > IIUC, the library would use IOCP on Windows, but I'm not familiar
> > with that. :-/
>
> Yep, iov feature could be removed probably. It doesn't brings much at
> the end.
>
> > Anyway, it can be used as base to develop a cross-platform WS
> > solution to be used with MHD.
>
> I would be happy if it could be done in the context of
> https://gitlab.com/jobol/lmhd-ws
>
> But it is a matter of choice I know. If you want please open issues to
> track your features: https://gitlab.com/jobol/lmhd-ws/issues
>
> Best regards
> José
>
> > Thank you!
> >
> > On Sun, Feb 3, 2019 at 12:56 AM silvioprog 
> > wrote:
> >
> > > Hello José,
> > >
> > > first, thanks for this awesome work! I took a look at the sources
> > > and it seems very clean. I'll test it on Windows. I saw you did the
> > > client too, so it will be very helpful for who those want to study
> > > WS.
> > >
> > > But, I have two questions: is it very difficult to do a minimal WS
> > > example to distribute in the MHD examples? If so, will mark the
> > > #5501  as 'won't fix' and
> > > close the issue. The other question is if it could be used as
> > > dependency in a library under LGPL license.
> > >
> > > Once more, thank you very much for sharing it! ☺
> > >
> > > On Fri, Feb 1, 2019 at 3:57 PM José Bollo  wrote:
> > >
> > >> I am proud to share with you a tiny implementation of websocket
> > >> with libmicrohttpd.
> > >>
> > >> You can find it there:
> > >>
> > >>https://gitlab.com/jobol/lmhd-ws
> > >>
> > >> I tried to give something small and useful (based on some previous
> > >> work I made).
> > >>
> > >> It is not perfect so feedback are welcome. Especially I only work
> > >> on linux. People on Mac or BSD are welcome to submit issues or pull
> > >> requests. People of windows also of course.
> > >>
> > >> Best regards
> > >> José Bollo
> > >
> > >
> > > --
> > > Silvio Clécio
> > >
> >
> >
>
>

-- 
Silvio Clécio


Re: [libmicrohttpd] tiny websocket above LMHD

2019-02-10 Thread silvioprog
Hello again José,

just to share two WS libraries I found:

1. https://github.com/tatsuhiro-t/wslay
2. https://libwebsockets.org

I have never tried these libraries before... maybe they could help to
create a cross-platform WS layer to use with MHD.

best,

On Sun, Feb 10, 2019 at 7:45 PM silvioprog  wrote:

> Hello José, thanks for answering.
>
> Sorry for resend, I don't know why the Gmail client sent it to your
> particular e-mail. 🤔
>
> So, I have a question. Could I send a feature-request at lmhd-ws/issues
> ?  If so, I'll open a new issue
> as feature-request to build the library on Windows using the MSYS2 tools
> http://www.msys2.org .
>
> Thank you! (y)
>
> On Tue, Feb 5, 2019 at 6:56 AM José Bollo  wrote:
>
>> On Sun, 3 Feb 2019 22:35:13 -0300
>> silvioprog  wrote:
>>
>> > Hello again José,
>>
>> Hi Silvio,
>>
>> > I tried the library on Linux and Windows. On Linux, it works, i.e.,
>> > the client sends text to the server, the only problem I've found was
>> > that Ctrl+C in the client closes the server too, but a server
>> > shouldn't be closed by a client.
>>
>> Perhaps some "signal(SIGPIPE, SIG_IGN);" is missing
>>
>> > On Windows (latest mingw-w64
>> > ), I couldn't compile it. I've got some errors
>> > like "array type has incomplete element type 'struct iovec'",
>> > "'F_SETFL' undeclared", "'O_NONBLOCK' undeclared", maybe by missing
>> > symbols specific from Linux that aren't present on Windows. I've
>> > tried to solve it, but it is a little bit hard to be solved, and
>> > IIUC, the library would use IOCP on Windows, but I'm not familiar
>> > with that. :-/
>>
>> Yep, iov feature could be removed probably. It doesn't brings much at
>> the end.
>>
>> > Anyway, it can be used as base to develop a cross-platform WS
>> > solution to be used with MHD.
>>
>> I would be happy if it could be done in the context of
>> https://gitlab.com/jobol/lmhd-ws
>>
>> But it is a matter of choice I know. If you want please open issues to
>> track your features: https://gitlab.com/jobol/lmhd-ws/issues
>>
>> Best regards
>> José
>>
>> > Thank you!
>> >
>> > On Sun, Feb 3, 2019 at 12:56 AM silvioprog 
>> > wrote:
>> >
>> > > Hello José,
>> > >
>> > > first, thanks for this awesome work! I took a look at the sources
>> > > and it seems very clean. I'll test it on Windows. I saw you did the
>> > > client too, so it will be very helpful for who those want to study
>> > > WS.
>> > >
>> > > But, I have two questions: is it very difficult to do a minimal WS
>> > > example to distribute in the MHD examples? If so, will mark the
>> > > #5501  as 'won't fix' and
>> > > close the issue. The other question is if it could be used as
>> > > dependency in a library under LGPL license.
>> > >
>> > > Once more, thank you very much for sharing it! ☺
>> > >
>> > > On Fri, Feb 1, 2019 at 3:57 PM José Bollo  wrote:
>> > >
>> > >> I am proud to share with you a tiny implementation of websocket
>> > >> with libmicrohttpd.
>> > >>
>> > >> You can find it there:
>> > >>
>> > >>https://gitlab.com/jobol/lmhd-ws
>> > >>
>> > >> I tried to give something small and useful (based on some previous
>> > >> work I made).
>> > >>
>> > >> It is not perfect so feedback are welcome. Especially I only work
>> > >> on linux. People on Mac or BSD are welcome to submit issues or pull
>> > >> requests. People of windows also of course.
>> > >>
>> > >> Best regards
>> > >> José Bollo
>> > >
>> > >
>> > > --
>> > > Silvio Clécio
>> > >
>> >
>> >
>>
>>
>
> --
> Silvio Clécio
>


-- 
Silvio Clécio