* limitations under the License.
*/
+#define USE_MAGIC_MIME_TYPE
#define _GNU_SOURCE
#include <stdlib.h>
#include <microhttpd.h>
+#if defined(USE_MAGIC_MIME_TYPE)
+#include <magic.h>
+#endif
+
#include "local-def.h"
#include "afb-method.h"
#include "afb-req-itf.h"
return 1;
}
+#if defined(USE_MAGIC_MIME_TYPE)
+
+#if !defined(MAGIC_DB)
+#define MAGIC_DB "/usr/share/misc/magic.mgc"
+#endif
+
+static magic_t lazy_libmagic()
+{
+ static int done = 0;
+ static magic_t result = NULL;
+
+ if (!done) {
+ done = 1;
+ /* MAGIC_MIME tells magic to return a mime of the file,
+ but you can specify different things */
+ if (verbosity)
+ printf("Loading mimetype default magic database\n");
+
+ result = magic_open(MAGIC_MIME_TYPE);
+ if (result == NULL) {
+ fprintf(stderr,"ERROR: unable to initialize magic library\n");
+ }
+ /* Warning: should not use NULL for DB
+ [libmagic bug wont pass efence check] */
+ else if (magic_load(result, MAGIC_DB) != 0) {
+ fprintf(stderr,"cannot load magic database - %s\n",
+ magic_error(result));
+ magic_close(result);
+ result = NULL;
+ }
+ }
+
+ return result;
+}
+
+static const char *magic_mimetype_fd(int fd)
+{
+ magic_t lib = lazy_libmagic();
+ return lib ? magic_descriptor(lib, fd) : NULL;
+}
+
+#endif
+
+
+
void afb_hreq_free(struct afb_hreq *hreq)
{
struct hreq_data *data;
#if defined(USE_MAGIC_MIME_TYPE)
/* set the type */
- if (hreq->session->magic) {
- const char *mimetype = magic_descriptor(hreq->session->magic, fd);
+ {
+ const char *mimetype = magic_mimetype_fd(fd);
if (mimetype != NULL)
MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
}
uuid = afb_hreq_get_argument(hreq, uuid_arg);
if (uuid == NULL)
uuid = afb_hreq_get_cookie(hreq, uuid_cookie);
- hreq->context = ctxClientGet(uuid);
+ hreq->context = ctxClientGetForUuid(uuid);
}
return hreq->context;
}