0

I am trying to create a server which will server pages in non English language, and i am testing libmicrohttpd with this code:

static int
answer_to_connection(void* cls, struct MHD_Connection* connection,
                     const char* url, const char* method,
                     const char* version, const char* upload_data,
                     size_t* upload_data_size, void** con_cls)
{
    char *page = "<html><head><meta charset='UTF-8'></head><body>हैलो यूनिकोड</body></html>";


    struct MHD_Response* response;
    int ret;

    response =
        MHD_create_response_from_buffer(strlen(page), (void *)page,
                                        MHD_RESPMEM_PERSISTENT);

    ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
    MHD_destroy_response(response);

    return ret;
}

But its not working and giving ????? characters on the browser. Can anyone tell me if libmicrohttpd supports Unicode, if yes then how?

Alok Saini
  • 321
  • 7
  • 18
  • i did that but no luck. – Alok Saini Dec 28 '16 at 11:11
  • Have a look here: http://stackoverflow.com/questions/1696619/displaying-unicode-symbols-in-html. Be sure your string is formatted in UTF-8. If you're using a C11 compiler prefix string with `u8` as: `char *page = u8"हैलो यूनिकोड";` – Frankie_C Dec 28 '16 at 11:26
  • yea that worked!!!! thank you, add this as an answer, i will mark as correct – Alok Saini Dec 28 '16 at 11:36

1 Answers1

2

As I already wrote in the comment you have to be sure that your string is formatted in UTF-8. Standard char type formats string as per the selected codepage or local, that gives bad formed characters if interpreted as UTF-8.

If you're using a C11 compiler prefix string with u8 as:

char *page = u8"<html><head><meta charset='UTF-8'></head><body>हैलो यूनिकोड</body></html>";

If your compiler doesn't support UTF-8 you need an external tool that formats the string using hex escaping or octal or the like.

Frankie_C
  • 4,764
  • 1
  • 13
  • 30