You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
3.8 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: soap-ctx.c,v 1.10 2006/07/09 16:24:19 snowdrop Exp $
  3. *
  4. * CSOAP Project: A SOAP client/server library in C
  5. * Copyright (C) 2003-2004 Ferhat Ayaz
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 02111-1307, USA.
  21. *
  22. * Email: ferhatayaz@jprogrammer.net
  23. ******************************************************************/
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #ifdef HAVE_STDIO_H
  28. #include <stdio.h>
  29. #endif
  30. #ifdef HAVE_STRING_H
  31. #include <string.h>
  32. #endif
  33. #ifdef HAVE_ERRNO_H
  34. #include <errno.h>
  35. #endif
  36. #include <nanohttp/nanohttp-logging.h>
  37. #include "soap-ctx.h"
  38. SoapCtx *
  39. soap_ctx_new(SoapEnv * env) /* should only be used internally */
  40. {
  41. SoapCtx *ctx;
  42. if (!(ctx = (SoapCtx *) malloc(sizeof(SoapCtx))))
  43. {
  44. log_error2("malloc failed (%s)", strerror(errno));
  45. return NULL;
  46. }
  47. ctx->env = env;
  48. ctx->attachments = NULL;
  49. ctx->action = NULL;
  50. return ctx;
  51. }
  52. void
  53. soap_ctx_add_files(SoapCtx * ctx, attachments_t * attachments)
  54. {
  55. part_t *part;
  56. char href[MAX_HREF_SIZE];
  57. if (attachments == NULL)
  58. return;
  59. part = attachments->parts;
  60. while (part)
  61. {
  62. soap_ctx_add_file(ctx, part->filename, part->content_type, href);
  63. part = part->next;
  64. }
  65. }
  66. herror_t
  67. soap_ctx_add_file(SoapCtx * ctx, const char *filename,
  68. const char *content_type, char *dest_href)
  69. {
  70. char cid[250];
  71. char id[250];
  72. part_t *part;
  73. static int counter = 1;
  74. FILE *test = fopen(filename, "r");
  75. if (!test)
  76. return herror_new("soap_ctx_add_file", FILE_ERROR_OPEN,
  77. "Can not open file '%s'", filename);
  78. fclose(test);
  79. /* generate an id */
  80. sprintf(id, "005512345894583%d", counter++);
  81. sprintf(dest_href, "cid:%s", id);
  82. sprintf(cid, "<%s>", id);
  83. /* add part to context */
  84. part = part_new(cid, filename, content_type, NULL, NULL);
  85. if (!ctx->attachments)
  86. ctx->attachments = attachments_new();
  87. attachments_add_part(ctx->attachments, part);
  88. return H_OK;
  89. }
  90. part_t *
  91. soap_ctx_get_file(SoapCtx * ctx, xmlNodePtr node)
  92. {
  93. xmlChar *prop;
  94. char href[MAX_HREF_SIZE];
  95. char buffer[MAX_HREF_SIZE];
  96. part_t *part;
  97. if (!ctx->attachments)
  98. return NULL;
  99. prop = xmlGetProp(node, "href");
  100. if (!prop)
  101. return NULL;
  102. strcpy(href, (const char *) prop);
  103. if (!strncmp(href, "cid:", 4))
  104. {
  105. for (part = ctx->attachments->parts; part; part = part->next)
  106. {
  107. sprintf(buffer, "<%s>", href + 4);
  108. if (!strcmp(part->id, buffer))
  109. return part;
  110. }
  111. }
  112. else
  113. {
  114. for (part = ctx->attachments->parts; part; part = part->next)
  115. {
  116. if (!strcmp(part->location, href))
  117. return part;
  118. }
  119. }
  120. return NULL;
  121. }
  122. void
  123. soap_ctx_free(SoapCtx * ctx)
  124. {
  125. if (!ctx)
  126. return;
  127. if (ctx->attachments)
  128. attachments_free(ctx->attachments);
  129. if (ctx->env)
  130. soap_env_free(ctx->env);
  131. if (ctx->action)
  132. free(ctx->action);
  133. free(ctx);
  134. return;
  135. }
  136. herror_t
  137. soap_ctx_new_with_method(const char *urn, const char *method, SoapCtx ** out)
  138. {
  139. SoapEnv *env;
  140. herror_t err;
  141. err = soap_env_new_with_method(urn, method, &env);
  142. if (err != H_OK)
  143. return err;
  144. *out = soap_ctx_new(env);
  145. return H_OK;
  146. }