summaryrefslogtreecommitdiffstats
path: root/libcsoap/csoaphttp.c
blob: 404d4b6754387bea1f831f319842fb66dfd794f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/******************************************************************
 *  $Id: csoaphttp.c,v 1.2 2003/11/13 10:44:10 snowdrop Exp $
 *
 * CSOAP Project:  A SOAP client/server library in C
 * Copyright (C) 2003  Ferhat Ayaz
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 * 
 * Email: ayaz@jprogrammet.net
 ******************************************************************/
#include "libcsoap/csoaphttp.h"
#include "libcsoap/csoaplog.h"
#include "libcsoap/csoapxml.h"

/*-----------------------------------------------------------------
  FUNCTION: SoapHttpCreateEnv
/-----------------------------------------------------------------*/

static
HSOAPENV SoapHttpCreateEnv(char* httpResponse)
{
  const char *FUNC = "SoapHttpCreateEnv";
  int count = 0;
  int newline = 0;
  xmlDocPtr env;
  xmlNodePtr node;
  int len;
  char *tmp;

  SoapTraceEnter(FUNC, "Entering response =\n");
  while (*(httpResponse+count) != '\0') {
    if (httpResponse[count]=='\r') {
      count++;
      continue;
    }
    if (httpResponse[count]=='\n') {
      if (newline) break;
      else newline = 1;
    } else newline = 0;

    count++;
  }

  len = strlen(httpResponse);
  SoapLog(LOG_DEBUG, FUNC, "count = %d\n", count);
  SoapLog(LOG_DEBUG, FUNC, "response length = %d\n", len);

  len = strlen(&httpResponse[count]);
  SoapLog(LOG_DEBUG, FUNC, "xml response length = %d\n", len);

  while (httpResponse[count] == '\n'
	 || httpResponse[count] == '\r') {
    count++;
  }
   
  SoapLog(LOG_DEBUG, FUNC, "count = %d\n", count);

  SoapLog(LOG_DEBUG, FUNC, "casting\n");
  tmp = (char*)(&httpResponse[count]);
  SoapLog(LOG_DEBUG, FUNC, "casting ok\n");

  /*  SoapLog(LOG_DEBUG, FUNC, "xml = %s\n",tmp?tmp:"null"); 
   */
  SoapLog(LOG_DEBUG, FUNC, "Starting parsing \n");
  env = xmlParseMemory((const char*)tmp, len);
  if (env == NULL) {
    SoapLog(LOG_ERROR, FUNC,
	    "Can not create xml document!");
    return NULL;
  }

  node = xmlDocGetRootElement(env);
  if (node == NULL) {
    SoapLog(LOG_ERROR, FUNC,
	    "xml document is empty!");
    return NULL;
  }

  SoapLog(LOG_DEBUG, FUNC,
	  "Envelope created successfully");

  SoapTraceLeave(FUNC, "env (%p)", node);
  return (HSOAPENV)node;
}


/*-----------------------------------------------------------------
  FUNCTION: SoapHttpSend
/-----------------------------------------------------------------*/

CSOAP_STATUS SoapHttpSend(HSOAPURL url, HSOAPENV env, HSOAPENV* res)
{
  const char *FUNC = "SoapHttpSend";
  HSOAPSOCKET http;
  int len;
  ssize_t size;
  char *tmp;
  char *response;
  char tmp2[1054];
  CSOAP_STATUS status;
  xmlBufferPtr buffer;
  xmlDocPtr doc;

  SoapTraceEnter(FUNC, "env (%p)", env);

  buffer = xmlBufferCreate();
  xmlNodeDump(buffer, ((xmlNodePtr)env)->doc, (xmlNodePtr)env, 1 ,1);
  len = strlen((const char*)xmlBufferContent(buffer));

  tmp = (char*)malloc(1054 + len);
  if (tmp == NULL)
    return ERROR_NO_MEM;

  status = SoapSocketCreate(url->m_host, url->m_port, &http);
  if (status != CSOAP_OK) return status;

  /*SoapSocketGetResponse(http);*/
  sprintf(tmp2, "POST %s HTTP/1.1\n", url->m_context?url->m_context:"/");
  sprintf(tmp2, "%sContent-Type: text/xml\n", tmp2);
  sprintf(tmp2, "%sContent-Length: %d\n", tmp2, len);
  sprintf(tmp2, "%sHost: %s\n\n", tmp2, url->m_host);

  sprintf(tmp, "%s%s", tmp2, (const char*)xmlBufferContent(buffer));
  /*printf((const char*)xmlBufferContent(buffer));*/

  SoapLog(LOG_DEBUG, FUNC, "sending request\n");
  status = SoapSocketSend(http, tmp, &size);
  if (status != CSOAP_OK) return status;

  status = SoapSocketRecv(http, &response, &size);
  SoapLog(LOG_DEBUG, FUNC, "response returned status = %d\n", status);
  SoapLog(LOG_DEBUG, FUNC, "Response string:'%s'", response);
  if (status != CSOAP_OK) return status;
  
  *res = SoapHttpCreateEnv(response);
  free(tmp);
  free(response);
  xmlBufferFree(buffer);

  SoapTraceLeave(FUNC, "");
  return CSOAP_OK;
}