Пример #1
0
BOOL cg_bittorrent_metainfo_save(CgBittorrentMetainfo *cbm, char *fileName)
{
	FILE *fp;
	CgString *str;

	if (!cbm)
		return FALSE;

	str = cg_string_new();
	if (!str)
		return FALSE;

	if (!cg_bittorrent_metainfo_tostring(cbm, str)) {
		cg_string_delete(str);
		return FALSE;
	}

	fp = fopen(fileName, "wb");
	if (fp) {
		fwrite(cg_string_getvalue(str), sizeof(char), cg_string_length(str), fp);
		fclose(fp);
	}

	cg_string_delete(str);

	return (fp ? TRUE : FALSE);
}
Пример #2
0
char *cg_string_replace(CgString *str, char *fromStr[], char *toStr[], size_t fromStrCnt)
{
	char *orgValue = NULL;
	size_t orgValueLen = 0;
	int n = 0;
	int copyPos = 0;
	size_t *fromStrLen = NULL;
	CgString *repValue = NULL;
	BOOL isReplaced = FALSE;

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

	if (NULL == str )
		return NULL;
	
	repValue = cg_string_new();
	
	fromStrLen = (size_t *)malloc(sizeof(size_t) * fromStrCnt);

	if ( NULL == fromStrLen )
	{
        cg_string_delete(repValue);
		cg_log_debug_s("Memory allocation failure!\n");
		return NULL;
	}
	
	for (n=0; n<fromStrCnt; n++)
		fromStrLen[n] = cg_strlen(fromStr[n]);
	
	orgValue = cg_string_getvalue(str);
	orgValueLen = cg_string_length(str);
	
	copyPos = 0;
	while (copyPos<orgValueLen) {
		isReplaced = FALSE;
		for (n=0; n<fromStrCnt; n++) {
			if (strncmp(fromStr[n], orgValue + copyPos,  fromStrLen[n]) == 0) {
				cg_string_addvalue(repValue, toStr[n]);
				copyPos += fromStrLen[n];
				isReplaced = TRUE;
				continue;
			}
		}
		if (isReplaced == TRUE)
			continue;
		cg_string_naddvalue(repValue, orgValue + copyPos, 1);
		copyPos++;
	}
	
	free(fromStrLen);

	cg_string_setvalue(str, cg_string_getvalue(repValue));	

	cg_string_delete(repValue);
		
	cg_log_debug_l5("Leaving...\n");

	return cg_string_getvalue(str);
}
Пример #3
0
void cg_http_header_delete(CgHttpHeader *header)
{
    cg_log_debug_l4("Entering...\n");

    cg_list_remove((CgList *)header);
    cg_string_delete(header->name);
    cg_string_delete(header->value);
    free(header);

    cg_log_debug_l4("Leaving...\n");
}
Пример #4
0
void cg_socket_datagram_packet_delete(CgDatagramPacket *dgmPkt)
{
	cg_log_debug_l4("Entering...\n");

	cg_string_delete(dgmPkt->data);
	cg_string_delete(dgmPkt->localAddress);
	cg_string_delete(dgmPkt->remoteAddress);

	free(dgmPkt);

	cg_log_debug_l4("Leaving...\n");
}
Пример #5
0
void cg_net_interface_delete(CgNetworkInterface *netIf)
{
	cg_log_debug_l4("Entering...\n");

	cg_list_remove((CgList *)netIf);
	cg_string_delete(netIf->name);
	cg_string_delete(netIf->ipaddr);
	cg_string_delete(netIf->netmask);

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

	free(netIf);
}
Пример #6
0
void cg_upnp_property_delete(CgUpnpProperty *prop)
{
    cg_log_debug_l4("Entering...\n");

    cg_upnp_property_clear(prop);
    cg_list_remove((CgList *)prop);

    cg_string_delete(prop->name);
    cg_string_delete(prop->value);
    cg_string_delete(prop->sid);

    free(prop);

    cg_log_debug_l4("Leaving...\n");
}
Пример #7
0
void cg_xml_node_delete(CgXmlNode *node)
{
	cg_log_debug_l4("Entering...\n");

	cg_list_remove((CgList *)node);
	cg_string_delete(node->name);
	cg_string_delete(node->value);
	cg_xml_attributelist_delete(node->attrList);
	cg_xml_nodelist_delete(node->nodeList);
	if (node->userDataDestructorFunc != NULL)
		node->userDataDestructorFunc(node->userData);
	free(node);

	cg_log_debug_l4("Leaving...\n");
}
Пример #8
0
void  cg_xml_node_print(CgXmlNode *node)
{
	CgString *str = cg_string_new();
	cg_xml_node_tostring(node, TRUE, str);
	printf("%s", cg_string_getvalue(str));
	cg_string_delete(str);
}
BOOL cg_upnp_ssdpresponse_server_post(CgUpnpSSDPResponseServer *server, CgUpnpSSDPRequest *ssdpReq)
{
	CgUpnpHttpUSocket *httpuSock;
	char *ifAddr;
	const char *ssdpAddr;
	CgString *ssdpMsg;
	size_t sentLen = 0;
	
	cg_log_debug_l4("Entering...\n");

	httpuSock = cg_upnp_ssdpresponse_server_getsocket(server);
	
	ifAddr = cg_socket_getaddress(httpuSock);
	ssdpAddr = cg_upnp_ssdp_gethostaddress(ifAddr);
	cg_upnp_ssdprequest_sethost(ssdpReq, ssdpAddr, CG_UPNP_SSDP_PORT);
		
	ssdpMsg = cg_string_new();
	cg_upnp_ssdprequest_tostring(ssdpReq, ssdpMsg);

	sentLen = cg_socket_sendto(httpuSock, ssdpAddr, CG_UPNP_SSDP_PORT, cg_string_getvalue(ssdpMsg), cg_string_length(ssdpMsg));
	cg_string_delete(ssdpMsg);
	
	cg_log_debug_l4("Leaving...\n");

	return (sentLen > 0);
}
Пример #10
0
/**
 * Destroy the given control point
 *
 * @param ctrlPoint The control point struct to destroy
 */
void cg_upnp_controlpoint_delete(CgUpnpControlPoint *ctrlPoint)
{
	cg_log_debug_l4("Entering...\n");

	cg_upnp_controlpoint_stop(ctrlPoint);
	
	/* Delete cached interfaces */
	cg_net_interfacelist_delete(ctrlPoint->ifCache);
	
	/* Delete expiration handlers */
	cg_thread_delete(ctrlPoint->expThread);
	cg_mutex_delete(ctrlPoint->expMutex);
	cg_cond_delete(ctrlPoint->expCond);
	
	/* Delete others */
	cg_mutex_delete(ctrlPoint->mutex);
	cg_xml_nodelist_delete(ctrlPoint->deviceRootNodeList);
	cg_upnp_devicelist_delete(ctrlPoint->deviceList);
	cg_upnp_ssdp_serverlist_delete(ctrlPoint->ssdpServerList);
	cg_upnp_ssdpresponse_serverlist_delete(ctrlPoint->ssdpResServerList);
	cg_http_serverlist_delete(ctrlPoint->httpServerList);
	cg_string_delete(ctrlPoint->httpEventURI);
	cg_upnp_eventlistenerlist_delete(ctrlPoint->eventListeners);	

#ifdef CG_HTTP_USE_PERSISTENT_CONNECTIONS	
	cg_http_persistentconnection_clear();
#endif
	free(ctrlPoint);

	cg_log_debug_l4("Leaving...\n");
}
Пример #11
0
void cg_http_packet_clean(CgHttpPacket *httpPkt)
{
	cg_log_debug_l4("Entering...\n");

	cg_http_headerlist_delete(httpPkt->headerList);
	cg_string_delete(httpPkt->content);

	cg_log_debug_l4("Leaving...\n");
}
Пример #12
0
void cg_http_request_delete(CgHttpRequest *httpReq)
{
    cg_log_debug_l4("Entering...\n");

    cg_http_packet_clean((CgHttpPacket *)httpReq);
    cg_string_delete(httpReq->method);
    cg_string_delete(httpReq->version);
    cg_string_delete(httpReq->uri);
    /* ADD Fabrice Fontaine Orange 23/04/2007 */
    /* Memory leak correction */
    cg_string_delete(httpReq->userAgent);
    /* ADD END Fabrice Fontaine Orange 23/04/2007 */
    cg_http_response_delete(httpReq->httpRes);
    cg_net_url_delete(httpReq->postURL);

    free(httpReq);

    cg_log_debug_l4("Leaving...\n");
}
void cg_upnp_event_subscription_request_settimeout(void *subReq, long timeout)
{
    CgString *timeoutBuf;

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

    timeoutBuf = cg_string_new();
    cg_http_packet_setheadervalue(((CgHttpPacket*)subReq), CG_HTTP_TIMEOUT, cg_upnp_event_subscription_totimeoutheaderstring(timeout, timeoutBuf));
    cg_string_delete(timeoutBuf);

    cg_log_debug_l4("Leaving...\n");
}
void cg_upnp_event_subscription_response_settimeout(CgUpnpSubscriptionResponse *subRes, long value)
{
	CgString *buf;

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

	buf = cg_string_new();
	cg_http_packet_setheadervalue((CgHttpPacket*)subRes, CG_HTTP_TIMEOUT, cg_upnp_event_subscription_totimeoutheaderstring(value, buf));
	cg_string_delete(buf);

	cg_log_debug_l4("Leaving...\n");
}
Пример #15
0
CgDictionary *cg_net_uri_getquerydictionary(CgNetURI *uri)
{
	char *query;
	int queryOffset;
	int eqIdx, ampIdx;
	CgString *paramName;
	CgString *paramValue;

	if (NULL == uri->queryDictionary)
		uri->queryDictionary = cg_dictionary_new();

	paramName = cg_string_new();
	paramValue = cg_string_new();

	query = cg_net_uri_getquery(uri);
	queryOffset = 0;

	eqIdx = cg_strstr(query, "=");
	while (0 < eqIdx) {
		ampIdx = cg_strstr(query + queryOffset, "&");
		if (ampIdx <= 0) {
			ampIdx = cg_strstr(query + queryOffset, "#");
			if (ampIdx <= 0)
				ampIdx = cg_strlen(query + queryOffset);
		}
		if (ampIdx <= eqIdx)
			break;
		cg_string_setnvalue(paramName, query + queryOffset, eqIdx);
		cg_string_setnvalue(paramValue, query + queryOffset + eqIdx + 1, (ampIdx - eqIdx -1));
		cg_dictionary_setvalue(uri->queryDictionary, cg_string_getvalue(paramName), cg_string_getvalue(paramValue));
		queryOffset += ampIdx + 1;
		eqIdx = cg_strstr(query + queryOffset, "=");
	}

	cg_string_delete(paramName);
	cg_string_delete(paramValue);

	return uri->queryDictionary;
}
Пример #16
0
void cg_upnp_statevariable_delete(CgUpnpStateVariable *statVar)
{
	cg_log_debug_l4("Entering...\n");

	cg_list_remove((CgList *)statVar);
	
	cg_string_delete(statVar->value);
	cg_upnp_status_delete(statVar->upnpStatus);
	
	free(statVar);

	cg_log_debug_l4("Leaving...\n");
}
Пример #17
0
static char *cg_xml_node_attribute_tostring(CgXmlNode *node, CgString *str)
{
	CgXmlAttribute *attr;
	char *name;
	char *value;
	CgString *valueStr;
	
	cg_log_debug_l4("Entering...\n");

	valueStr = cg_string_new();
	if (valueStr == NULL) return NULL;

	for (attr = cg_xml_node_getattributes(node); attr != NULL; attr = cg_xml_attribute_next(attr)) {
		name = cg_xml_attribute_getname(attr);
		value = cg_xml_attribute_getvalue(attr);
		
		cg_string_setvalue(valueStr, value);
		cg_xml_escapechars(valueStr);

		/* All the following functions return NULL only when memory 
		   allocation fails, so we can check them all */
		if (!cg_string_naddvalue(str, " ", 1) || 
		    !cg_string_addvalue(str, name) ||
		    !cg_string_naddvalue(str, "=\"", 2) ||
		    !cg_string_addvalue(str, cg_string_getvalue(valueStr)) ||
		    !cg_string_naddvalue(str, "\"", 1))
		{
			/* Memory allocation failed */
			cg_string_delete(valueStr);
			return NULL;
		}
	}
	cg_string_delete(valueStr);
	
	cg_log_debug_l4("Leaving...\n");

	return cg_string_getvalue(str);
}
Пример #18
0
int cg_socket_delete(CgSocket *sock)
{
	cg_log_debug_l4("Entering...\n");

	cg_socket_close(sock);
	cg_string_delete(sock->ipaddr);
#if defined(ITRON)
	cg_socket_freewindowbuffer(sock);
#endif
	free(sock);
	cg_socket_cleanup();

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

	return 0;
}
Пример #19
0
void cg_net_uri_delete(CgNetURI *uri)
{
	cg_log_debug_l4("Entering...\n");

	cg_string_delete(uri->uri);
	cg_string_delete(uri->protocol);
	cg_string_delete(uri->user);
	cg_string_delete(uri->password);
	cg_string_delete(uri->host);
	cg_string_delete(uri->path);
	cg_string_delete(uri->query);
	cg_string_delete(uri->fragment);

	if (uri->request != NULL)
		cg_string_delete(uri->request);
	if (uri->queryDictionary != NULL)
		cg_dictionary_delete(uri->queryDictionary);

	free(uri);

	cg_log_debug_l4("Leaving...\n");
}
void cg_http_persistentconnection_delete(CgHttpPersistentConnection *node)
{
	cg_log_debug_l4("Entering...\n");

       cg_string_delete(node->host);

       /* Terminate and delete connection according libcurl usage */
#if !defined(CG_HTTP_CURL)
       cg_socket_close((CgSocket *)node->cacheData);
       cg_socket_delete((CgSocket *)node->cacheData);
#else
       curl_easy_cleanup((CURL *)node->cacheData);
#endif
       free(node);

	cg_log_debug_l4("Leaving...\n");
}
Пример #21
0
BOOL cg_upnp_ssdp_socket_postresponse(CgUpnpSSDPSocket *ssdpSock, CgUpnpSSDPResponse *ssdpRes, const char *host, int port)
{
	CgString *ssdpMsg;
  size_t ssdpMsgLen;
	BOOL postSuccess;

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

	ssdpMsg = cg_string_new();
	cg_upnp_ssdpresponse_tostring(ssdpRes, ssdpMsg);
  ssdpMsgLen = cg_string_length(ssdpMsg);
	postSuccess = (cg_socket_sendto(ssdpSock, host, port, cg_string_getvalue(ssdpMsg), ssdpMsgLen) == ssdpMsgLen) ? TRUE : FALSE;
	cg_string_delete(ssdpMsg);

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

	return postSuccess;
}
Пример #22
0
static BOOL cg_upnp_ssdp_socket_notify(CgUpnpSSDPSocket *ssdpSock, CgUpnpSSDPRequest *ssdpReq, const char *ssdpAddr)
{
	CgString *ssdpMsg;
	size_t sentLen;

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

	cg_upnp_ssdprequest_setmethod(ssdpReq, CG_HTTP_NOTIFY);
	
	ssdpMsg = cg_string_new();
	cg_upnp_ssdprequest_tostring(ssdpReq, ssdpMsg);

	sentLen = cg_socket_sendto(ssdpSock, ssdpAddr, CG_UPNP_SSDP_PORT, cg_string_getvalue(ssdpMsg), cg_string_length(ssdpMsg));
	cg_string_delete(ssdpMsg);
	
	cg_log_debug_l4("Leaving...\n");

	return (0 < sentLen) ? TRUE : FALSE;
}
Пример #23
0
void cg_upnpav_resource_updateattributes(CgUpnpAvResource *res)
{
    CgUpnpAvResourceData *nodeData;
    CgString *resAttr;
    char *mimeType;
    char *dlnaAttr;

    nodeData = (CgUpnpAvResourceData *)cg_xml_node_getuserdata(res);
    mimeType = (0 < cg_string_length(nodeData->mimeType)) ? cg_string_getvalue(nodeData->mimeType) : "*/*";
    dlnaAttr = (0 < cg_string_length(nodeData->dlnaAttr)) ? cg_string_getvalue(nodeData->dlnaAttr) : "*";

    resAttr = cg_string_new();
    cg_string_addvalue(resAttr, "http-get:*:");
    cg_string_addvalue(resAttr, mimeType);
    cg_string_addvalue(resAttr, ":");
    cg_string_addvalue(resAttr, dlnaAttr);
    cg_xml_node_setattribute(res, CG_UPNPAV_RESOURCE_PROTOCOLINFO, cg_string_getvalue(resAttr));
    cg_string_delete(resAttr);
}
void cg_upnp_event_subscription_request_setsid(CgUpnpSubscriptionRequest *subReq, const char *sid)
{
    CgString *headerSID;
    ssize_t uuidIdx;

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

    headerSID = cg_string_new();

    uuidIdx = cg_strstr(sid, CG_UPNP_ST_UUID_DEVICE);
    if (uuidIdx < 0)
        cg_string_addvalue(headerSID, CG_UPNP_ST_UUID_DEVICE ":");
    cg_string_addvalue(headerSID, sid);

    cg_http_packet_setheadervalue(((CgHttpPacket*)subReq), CG_HTTP_SID, cg_string_getvalue(headerSID));

    cg_string_delete(headerSID);

    cg_log_debug_l4("Leaving...\n");
}
Пример #25
0
void cg_net_uri_clear(CgNetURI *uri)
{
	cg_log_debug_l4("Entering...\n");

	cg_string_clear(uri->uri);
	cg_string_clear(uri->protocol);
	cg_string_clear(uri->user);
	cg_string_clear(uri->password);
	cg_string_clear(uri->host);
	uri->port = 0;
	cg_string_clear(uri->path);
	cg_string_clear(uri->query);
	cg_string_clear(uri->fragment);
	if (uri->request != NULL) 
	{
		cg_string_delete(uri->request);
		uri->request = NULL;
	}

	cg_log_debug_l4("Leaving...\n");
}
Пример #26
0
BOOL cg_upnpav_dms_conmgr_actionreceived(CgUpnpAction* action)
{
  CgUpnpAvServer* dms;
  CgUpnpDevice* dev;
  char* actionName;
  CgUpnpArgument* arg;
  CgString* protocolInfos;
  CgUpnpAvProtocolInfo* protocolInfo;

  actionName = (char*)cg_upnp_action_getname(action);
  if (cg_strlen(actionName) <= 0)
    return FALSE;

  dev = (CgUpnpDevice*)cg_upnp_service_getdevice(cg_upnp_action_getservice(action));
  if (!dev)
    return FALSE;

  dms = (CgUpnpAvServer*)cg_upnp_device_getuserdata(dev);
  if (!dms)
    return FALSE;

  /* GetProtocolInfo*/
  if (cg_streq(actionName, CG_UPNPAV_DMS_CONNECTIONMANAGER_GET_PROTOCOL_INFO)) {
    arg = cg_upnp_action_getargumentbyname(action, CG_UPNPAV_DMS_CONNECTIONMANAGER_SOURCE);
    if (!arg)
      return FALSE;
    protocolInfos = cg_string_new();
    for (protocolInfo = cg_upnpav_dms_getprotocolinfos(dms); protocolInfo; protocolInfo = cg_upnpav_protocolinfo_next(protocolInfo)) {
      if (0 < cg_string_length(protocolInfos))
        cg_string_addvalue(protocolInfos, ",");
      cg_string_addvalue(protocolInfos, cg_upnpav_protocolinfo_getstring(protocolInfo));
    }
    cg_upnp_argument_setvalue(arg, cg_string_getvalue(protocolInfos));
    cg_string_delete(protocolInfos);
    return TRUE;
  }

  return FALSE;
}
Пример #27
0
void cg_upnpav_dmr_addprotocolinfo(CgUpnpAvRenderer* dmr, CgUpnpAvProtocolInfo* info)
{
  CgString* protocolInfos;
  CgUpnpAvProtocolInfo* protocolInfo;
  CgUpnpService* service;
  CgUpnpStateVariable* stateVar;

  cg_upnpav_protocolinfolist_add(dmr->protocolInfoList, info);

  protocolInfos = cg_string_new();
  for (protocolInfo = cg_upnpav_dmr_getprotocolinfos(dmr); protocolInfo; protocolInfo = cg_upnpav_protocolinfo_next(protocolInfo)) {
    if (0 < cg_string_length(protocolInfos))
      cg_string_addvalue(protocolInfos, ",");
    cg_string_addvalue(protocolInfos, cg_upnpav_protocolinfo_getstring(protocolInfo));
  }

  service = cg_upnp_device_getservicebyexacttype(dmr->dev, CG_UPNPAV_DMR_CONNECTIONMANAGER_SERVICE_TYPE);
  stateVar = cg_upnp_service_getstatevariablebyname(service, CG_UPNPAV_DMR_CONNECTIONMANAGER_SINKPROTOCOLINFO);
  cg_upnp_statevariable_setvalue(stateVar, cg_string_getvalue(protocolInfos));

  cg_string_delete(protocolInfos);
}
Пример #28
0
void cg_net_uri_setvalue(CgNetURI *uri, char *value)
{
	char *protocol;
	int uriLen;
	int currIdx;
	int protoIdx;
	int atIdx;
	int colonIdx;
	int shashIdx;
	char *host;
	int eblacketIdx;
	CgString *hostStr;
	CgString *portStr;
	int hostLen;
	int sharpIdx;
	int questionIdx;
	int queryLen;
	
	cg_log_debug_l4("Entering...\n");

	uriLen = cg_strlen(value);
	cg_net_uri_clear(uri);
	cg_net_uri_seturi(uri, value);
		
	currIdx = 0;
	
	/*** Protocol ****/
	protoIdx = cg_strstr(value, CG_NET_URI_PROTOCOL_DELIM);
	if (0 < protoIdx) {
		cg_string_setnvalue(uri->protocol, value,  protoIdx);
		currIdx += protoIdx + cg_strlen(CG_NET_URI_PROTOCOL_DELIM);
	}

	/*** User (Password) ****/
	atIdx = cg_strstr(value+currIdx, CG_NET_URI_USER_DELIM);
	if (0 < atIdx) {
		colonIdx = cg_strstr(value+currIdx, CG_NET_URI_COLON_DELIM);
		/**** Thanks for Theo Beisch (2005/08/25) ****/
		if (0 < colonIdx && colonIdx<atIdx) {
			cg_string_setnvalue(uri->user, value+currIdx,  colonIdx);
			cg_string_setnvalue(uri->password, value+currIdx+colonIdx+1,  atIdx-(colonIdx+1));
		}
		else 
			cg_string_setnvalue(uri->user, value+currIdx,  atIdx - currIdx);
		currIdx += atIdx + 1;
	}

	/*** Host (Port) ****/
	shashIdx = cg_strstr(value+currIdx, CG_NET_URI_SLASH_DELIM);
	if (0 < shashIdx)
		cg_string_setnvalue(uri->host, value+currIdx,  shashIdx);
	else if (cg_net_uri_isabsolute(uri) == TRUE)
		cg_string_setnvalue(uri->host, value+currIdx, cg_strlen(value) - currIdx);
	host = cg_net_uri_gethost(uri);
	colonIdx = cg_strrchr(host, CG_NET_URI_COLON_DELIM, 1);
	eblacketIdx = cg_strrchr(host, CG_NET_URI_EBLACET_DELIM, 1);
	if (0 < colonIdx && eblacketIdx < colonIdx) {
		hostStr = cg_string_new();
		cg_string_setvalue(hostStr, host);
		hostLen = cg_string_length(hostStr);
		/**** host ****/
		cg_string_setnvalue(uri->host, cg_string_getvalue(hostStr),  colonIdx);
		host = cg_net_uri_gethost(uri);
		if (0 < hostLen) {
			if (host[0] == '[' && host[hostLen-1] == ']')
				cg_string_setnvalue(uri->host, cg_string_getvalue(hostStr)+1,  colonIdx-2);
		}
		/**** port ****/
		portStr = cg_string_new();
		cg_string_setnvalue(portStr, cg_string_getvalue(hostStr)+colonIdx+1,  hostLen- colonIdx-1);
		uri->port = atoi(cg_string_getvalue(portStr));
		cg_string_delete(portStr);
		cg_string_delete(hostStr);
	}
	else {
		uri->port = CG_NET_URI_KNKOWN_PORT;
		protocol = cg_net_uri_getprotocol(uri);
		if (cg_strcmp(protocol, CG_NET_URI_PROTOCOL_HTTP) == 0)
			uri->port = CG_NET_URI_DEFAULT_HTTP_PORT;
		if (cg_strcmp(protocol, CG_NET_URI_PROTOCOL_FTP) == 0)
			uri->port = CG_NET_URI_DEFAULT_FTP_PORT;
	}
	
	if (shashIdx > 0) currIdx += shashIdx;
	
	/*
		Handle relative URL
	*/
	if (cg_net_uri_isabsolute(uri) == FALSE)
	{
		cg_string_addvalue(uri->path, value);
		
	} else {
		/* First set path simply to the rest of URI */
		cg_string_setnvalue(uri->path, value+currIdx,  uriLen-currIdx);
	}
		
	/**** Path (Query/Fragment) ****/
	sharpIdx = cg_strstr(value+currIdx, CG_NET_URI_SHARP_DELIM);
	if (0 < sharpIdx) {
		cg_string_setnvalue(uri->path, value+currIdx,  sharpIdx);
		cg_string_setnvalue(uri->fragment, value+currIdx+sharpIdx+1,  uriLen-(currIdx+sharpIdx+1));
	}
	questionIdx = cg_strstr(value+currIdx, CG_NET_URI_QUESTION_DELIM);
	if (0 < questionIdx) {
		cg_string_setnvalue(uri->path, value+currIdx,  questionIdx);
		queryLen = uriLen-(currIdx+questionIdx+1);
		if (0 < sharpIdx)
			queryLen -= uriLen - (currIdx+sharpIdx);
		cg_string_setnvalue(uri->query, value+currIdx+questionIdx+1,  queryLen);
	}

	cg_log_debug_l4("Leaving...\n");
}
Пример #29
0
CgHttpResponse *cg_http_request_post(CgHttpRequest *httpReq, char *ipaddr, int port)
{
    CgHttpResponse *httpRes;
    BOOL newCurl = FALSE;
    CURL *curl;
    CgHttpHeader *reqHeader;
    struct curl_slist *curlHeaderList;
    CgString *headerStr;
    CURLcode res;
    char *uri, *method;
    char url[CG_NET_URI_MAXLEN];
    long retcode;
#ifdef CG_SHOW_TIMINGS
    struct timeval start_time, end_time, elapsed_time;
#endif

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

#ifdef CG_SHOW_TIMINGS
    gettimeofday(&start_time, NULL);
#endif

    httpRes = httpReq->httpRes;

    /* Clear the response data because new data will not
     * overwrite it, but it is appended to the end */
    cg_string_clear(httpRes->content);

    cg_log_debug_s("Posting HTTP request (Curl)\n");
    cg_http_request_print(httpReq);

    cg_http_persistentconnection_lock();
#ifdef CG_HTTP_USE_PERSISTENT_CONNECTIONS
    cg_log_debug_s("Looking for persistent connection to %s, port %d\n", ipaddr, port);
    curl = (CURL*)cg_http_persistentconnection_get(ipaddr, port);

    if (curl == NULL)
    {
        cg_log_debug_s("Persistent connection not found...\n");
#endif
        curl = curl_easy_init();
        if (curl == NULL)
        {
            cg_http_persistentconnection_unlock();
            return httpReq->httpRes;
        }
#ifdef CG_HTTP_USE_PERSISTENT_CONNECTIONS
        newCurl = TRUE;
    }
#endif
    method = cg_http_request_getmethod(httpReq);
    uri = cg_http_request_geturi(httpReq);

    /**** method ****/
    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method);

    /**** url ****/
    if (uri && cg_strstr(uri, CG_NET_URI_PROTOCOL_DELIM) > 0)
    {
        curl_easy_setopt(curl, CURLOPT_URL, uri);
    }
    else
    {
        cg_net_gethosturl(ipaddr, port, uri, url, sizeof(url));
        curl_easy_setopt(curl, CURLOPT_URL, url);
        cg_log_debug_s("\n\nCURL: %s\n\n", url);
    }

    /**** header ****/
    curlHeaderList = NULL;
    headerStr = cg_string_new();
    for (reqHeader = cg_http_request_getheaders(httpReq); reqHeader; reqHeader = cg_http_header_next(reqHeader)) {
        cg_string_setvalue(headerStr, cg_http_header_getname(reqHeader));
        if (cg_string_addvalue(headerStr, CG_HTTP_COLON CG_HTTP_SP) &&
                cg_string_addvalue(headerStr, cg_http_header_getvalue(reqHeader)))
            curlHeaderList = curl_slist_append(curlHeaderList, cg_string_getvalue(headerStr));
    }
    cg_string_delete(headerStr);
    /* Disable Expect header because it causes IOP issues */
    curlHeaderList = curl_slist_append(curlHeaderList, "Expect:");
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curlHeaderList);

    /**** content ****/
    /*if (cg_http_request_ispostrequest(httpReq) == TRUE) {*/
    if (cg_http_request_getcontentlength(httpReq) > 0) {
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, cg_http_request_getcontent(httpReq));
        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, cg_http_request_getcontentlength(httpReq));
    }
    else
    {
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, NULL);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0);
    }

    /* This has to be enabled for progress callback to be called */
    curl_easy_setopt(curl, CURLOPT_NOPROGRESS,
                     FALSE);

    /* Used for checking stack state during curl easy perform */
    curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION,
                     cg_http_request_progress_callback);

    /**** response header callback ****/
    curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION,
                     cg_http_request_header_callback);
    curl_easy_setopt(curl, CURLOPT_WRITEHEADER, (void *)httpRes);

    /**** response content callback ****/
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
                     cg_http_request_content_callback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)httpRes);

    /**** useragent ****/
    curl_easy_setopt(curl, CURLOPT_USERAGENT, cg_http_request_getuseragent(httpReq) );

    /**** Prohibit curl from using signals ****/
    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);

    /**** Set the connection timeout so we don't wait forever ****/
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT,
                     CG_HTTP_CURL_CONNECTTIMEOUT);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT,
                     CG_HTTP_CONN_TIMEOUT);
#ifdef CG_SHOW_TIMINGS
    cg_log_debug_s("\nRequest: %s%s%s\n", method, CG_HTTP_SP, url);
#endif

    /* Get the XML document with CURL */
    res = curl_easy_perform(curl);
    if (res != CURLE_OK)
        cg_log_debug_s("curl_easy_perform: %s\n",
                       curl_easy_strerror(res));

    /* Set the content length, if it wasn't said in the header */
    if (cg_http_response_getcontentlength(httpRes) <= 0)
    {
        cg_http_response_setcontentlength(httpRes,
                                          cg_string_length(httpRes->content));
    }

    curl_slist_free_all(curlHeaderList);

    curl_easy_getinfo (curl, CURLINFO_HTTP_CODE, &retcode);
    cg_http_response_setstatuscode(httpRes, retcode);

#ifdef CG_SHOW_TIMINGS
    gettimeofday(&end_time, NULL);
    timersub(&end_time, &start_time, &elapsed_time);
    cg_log_debug_s("Getting HTTP-response completed. Elapsed time: "
                   "%ld msec\n", ((elapsed_time.tv_sec*1000) +
                                  (elapsed_time.tv_usec/1000)));
    cg_total_elapsed_time += (elapsed_time.tv_sec*1000000)+
                             (elapsed_time.tv_usec);
#endif

#ifdef CG_HTTP_USE_PERSISTENT_CONNECTIONS
    if (newCurl)
    {
        cg_log_debug_s("Putting new connection into cache: %s %d\n", ipaddr, port);
        cg_http_persistentconnection_put(ipaddr, port, curl);
    }
#else
    curl_easy_cleanup(curl);
#endif
    cg_http_persistentconnection_unlock();

    cg_log_debug_s("Response for HTTP request (Curl)\n");
    cg_http_response_print(httpReq->httpRes);

    return httpReq->httpRes;

    cg_log_debug_l4("Leaving...\n");
}
Пример #30
0
static char *cg_xml_node_tostring_indent(CgXmlNode *node, int indentLevel, BOOL withChildNode, CgString *str)
{
	char *name;
	char *value;
	CgString *valueStr;
	CgXmlNode *childNode;
	
	cg_log_debug_l4("Entering...\n");

	name = cg_xml_node_getname(node);
	value = cg_xml_node_getvalue(node);

	if (cg_xml_node_haschildnodes(node) == FALSE || withChildNode == FALSE) {
		cg_string_addrepvalue(str, CG_XML_INDENT_STRING, indentLevel);
		if (!cg_string_naddvalue(str, "<", 1) ||
		    !cg_string_addvalue(str, name) ||
		    !cg_xml_node_attribute_tostring(node, str))
			/* Memory allocation failed */
			return NULL;
		
		valueStr = cg_string_new();
		if (!valueStr)
			/* Memory allocation failed */
			return NULL;
		
		cg_string_setvalue(valueStr, value);
		cg_xml_escapechars(valueStr);

		if (!cg_string_naddvalue(str, ">", 1) ||
		    !cg_string_addvalue(str, cg_string_getvalue(valueStr)) ||
		    !cg_string_naddvalue(str, "</", 2) ||
		    !cg_string_addvalue(str, name) ||
		    !cg_string_naddvalue(str, ">", 1) ||
		    !cg_string_addvalue(str, "\n"))
		{
			/* Memory allocation failed */
			cg_string_delete(valueStr);
			return NULL;
		}

		cg_string_delete(valueStr);
		
		return cg_string_getvalue(str);
	}

	cg_string_addrepvalue(str, CG_XML_INDENT_STRING, indentLevel);
	if (!cg_string_naddvalue(str, "<", 1) ||
	    !cg_string_addvalue(str, name) ||
	    !cg_xml_node_attribute_tostring(node, str) ||
	    !cg_string_naddvalue(str, ">", 1) ||
	    !cg_string_addvalue(str, "\n"))
		/* Memory allocation failed */
		return NULL;

	for (childNode = cg_xml_node_getchildnodes(node); childNode != NULL; childNode = cg_xml_node_next(childNode))
		if (!cg_xml_node_tostring_indent(childNode, indentLevel+1, TRUE, str))
			/* Memory allocation failed */
			return NULL;

	cg_string_addrepvalue(str, CG_XML_INDENT_STRING, indentLevel);
	if (!cg_string_naddvalue(str, "</", 2) ||
	    !cg_string_addvalue(str, name) ||
	    !cg_string_naddvalue(str, ">", 1) ||
	    !cg_string_addvalue(str, "\n"))
		/* Memory allocation failed */
		return NULL;

	cg_log_debug_l4("Leaving...\n");
	
	return cg_string_getvalue(str);
}