示例#1
0
bool mupnp_http_response_read(mUpnpHttpResponse* httpRes, mUpnpSocket* sock, bool onlyHeader)
{
  char lineBuf[MUPNP_HTTP_READLINE_BUFSIZE];
  mUpnpStringTokenizer* strTok;
  char* token;
  ssize_t readLen;

  mupnp_log_debug_l4("Entering...\n");

  mupnp_http_response_clear(httpRes);

  readLen = mupnp_socket_readline(sock, lineBuf, sizeof(lineBuf));
  if (readLen <= 0)
    return false;

  strTok = mupnp_string_tokenizer_new(lineBuf, MUPNP_HTTP_STATUSLINE_DELIM);
  if (mupnp_string_tokenizer_hasmoretoken(strTok) == true)
    mupnp_http_response_setversion(httpRes, mupnp_string_tokenizer_nexttoken(strTok));
  if (mupnp_string_tokenizer_hasmoretoken(strTok) == true)
    mupnp_http_response_setstatuscode(httpRes, atoi(mupnp_string_tokenizer_nexttoken(strTok)));
  if (mupnp_string_tokenizer_hasmoretoken(strTok) == true) {
    token = mupnp_string_tokenizer_nextalltoken(strTok);
    mupnp_strrtrim(token, MUPNP_HTTP_STATUSLINE_DELIM, mupnp_strlen(MUPNP_HTTP_STATUSLINE_DELIM));
    mupnp_http_response_setreasonphrase(httpRes, token);
  }
  mupnp_string_tokenizer_delete(strTok);

  mupnp_http_packet_read((mUpnpHttpPacket*)httpRes, sock, onlyHeader, lineBuf, sizeof(lineBuf));

  mupnp_log_debug_l4("Leaving...\n");

  return true;
}
示例#2
0
void mupnp_ssdp_packet_setheader(mUpnpSSDPPacket *ssdpPkt, char *ssdpMsg)
{
	mUpnpStringTokenizer *ssdpTok;
	mUpnpStringTokenizer *ssdpLineTok;
	mUpnpHttpHeader *header;
	char *lineMsg;
	char *name;
	char *value;
		
	mupnp_log_debug_l4("Entering...\n");

	ssdpTok = mupnp_string_tokenizer_new(ssdpMsg, MUPNP_HTTP_CRLF);

	/**** skip the first line ****/	
	if (mupnp_string_tokenizer_hasmoretoken(ssdpTok) == false)
		return;
	lineMsg = mupnp_string_tokenizer_nexttoken(ssdpTok);
	
	while (mupnp_string_tokenizer_hasmoretoken(ssdpTok) == true) {
		lineMsg = mupnp_string_tokenizer_nexttoken(ssdpTok);
		name = NULL;
		value = NULL;
		ssdpLineTok = mupnp_string_tokenizer_new(lineMsg, MUPNP_HTTP_HEADERLINE_DELIM);
		if (mupnp_string_tokenizer_hasmoretoken(ssdpLineTok) == true)
			name = mupnp_string_tokenizer_nexttoken(ssdpLineTok);
		if (mupnp_string_tokenizer_hasmoretoken(ssdpLineTok) == true) {
			value = mupnp_string_tokenizer_nextalltoken(ssdpLineTok);
			mupnp_strrtrim(value, MUPNP_HTTP_HEADERLINE_DELIM, mupnp_strlen(MUPNP_HTTP_HEADERLINE_DELIM));
		}
		if (name != NULL) {
			if (value == NULL)
				value = "";
			header = mupnp_http_header_new();
			mupnp_http_header_setname(header, name);
			mupnp_http_header_setvalue(header, value);
			mupnp_http_headerlist_add(ssdpPkt->headerList, header);
		}
		mupnp_string_tokenizer_delete(ssdpLineTok);
	}

	mupnp_string_tokenizer_delete(ssdpTok);

	mupnp_log_debug_l4("Leaving...\n");
}
示例#3
0
mUpnpXmlNode *mupnp_soap_request_getbodynode(mUpnpSoapRequest *soapReq)
{
	mUpnpXmlNode *envNode;
	mUpnpXmlNode *bodyNode = NULL;
  mUpnpXmlAttribute *attr;
  char *name;
  mUpnpStringTokenizer *tok;
  char *nsPrefix;
  size_t bodyLen;
  char *body;

	mupnp_log_debug_l4("Entering...\n");

	envNode = mupnp_soap_request_getenvelopenode(soapReq);
	if (envNode == NULL)
		return NULL;
	if (mupnp_xml_node_haschildnodes(envNode) == false)
		return NULL;

        /* We cannot assume the namespace prefix for Body is 's'. 
           According to spec, it could be anything... */
        for (attr = mupnp_xml_node_getattributes(envNode); attr != NULL; 
             attr = mupnp_xml_attribute_next(attr)) {
                /* First, find the namespace declaration attribute. */
                /* Note: We must take a copy of the attr name. 
                   Tokenizer doesn't do it (by default) */
                name = mupnp_strdup( mupnp_xml_attribute_getname(attr) );
                tok = mupnp_string_tokenizer_new(name, ":");

                nsPrefix = mupnp_string_tokenizer_nexttoken(tok);
                if ( -1 != mupnp_strstr(nsPrefix, "xmlns")) {
                        /* This attribute is a namespace declaration. Check is 
                           it the one defined for SOAP. */
                        if (mupnp_strcmp(mupnp_xml_attribute_getvalue(attr), MUPNP_SOAP_XMLNS_URL) == 0) {
                                /* This namespace declaration is correct. 
                                   Use it to find the body node... */
                                if (mupnp_string_tokenizer_hasmoretoken(tok)) {
                                        /* There is a prefix */
                                        nsPrefix = mupnp_string_tokenizer_nexttoken(tok);
                                        bodyLen = mupnp_strlen(nsPrefix) + 
                                                mupnp_strlen(MUPNP_SOAP_DELIM) + 
                                                mupnp_strlen(MUPNP_SOAP_BODY) + 1; /* +1 for trailing '\0'*/
                                        body = (char*)malloc(bodyLen);

					if ( NULL == body )
					{
						mupnp_log_debug_s("Memory allocation failure!\n");
						return NULL;
					}
#if defined(HAVE_SNPRINTF)
                                        snprintf(body, bodyLen, "%s%s%s", nsPrefix, 
                                                 MUPNP_SOAP_DELIM, MUPNP_SOAP_BODY);
#else
                                        sprintf(body, "%s%s%s", nsPrefix, MUPNP_SOAP_DELIM, MUPNP_SOAP_BODY);
#endif
                                        bodyNode = mupnp_xml_node_getchildnode(envNode, body);
                                        free(body);
                                }
                                else {
                                        /* No prefix */
                                        bodyNode = mupnp_xml_node_getchildnode(envNode, MUPNP_SOAP_BODY);
                                }
                                /* Free memory before leaving the loop */
                                mupnp_string_tokenizer_delete(tok);
                                free(name);
                                break;
                        }
                }
                mupnp_string_tokenizer_delete(tok);
                free(name);
        }

	mupnp_log_debug_l4("Leaving...\n");
	
	return bodyNode;
}