35 #ifndef _PLT_UTILITIES_H_ 36 #define _PLT_UTILITIES_H_ 58 const char* namespc) :
59 m_Element(element), m_Name(name), m_Namespace(namespc) {}
62 if (attribute->GetName() == m_Name) {
64 const NPT_String& prefix = attribute->GetPrefix();
65 if (m_Namespace[0] ==
'\0') {
67 return prefix.IsEmpty();
71 const NPT_String* namespc = m_Element.GetNamespaceUri(prefix);
72 return namespc && *namespc == m_Namespace;
86 const char* m_Namespace;
109 NPT_Result result = parser.Parse(xml, node);
110 if (NPT_FAILED(result)) {
115 tree = node->AsElementNode();
127 const char* namespc =
"",
128 NPT_Cardinal max_size = 1024) {
131 if (!node)
return NPT_FAILURE;
134 if (namespc && namespc[0] ==
'\0') {
135 namespc = node->GetNamespace()?node->GetNamespace()->GetChars():NPT_XML_NO_NAMESPACE;
139 if (!child)
return NPT_FAILURE;
144 value = text->SubString(0, max_size);
150 const char* namespc =
"") {
151 if (!node)
return NPT_FAILURE;
154 if (namespc && namespc[0] ==
'\0') {
155 namespc = node->GetNamespace()?node->GetNamespace()->GetChars():NPT_XML_NO_NAMESPACE;
160 if (!attribute)
return NPT_FAILURE;
163 NPT_CHECK(node->GetAttributes().Erase(attribute));
171 const char* namespc =
"") {
174 if (!node)
return NPT_FAILURE;
177 if (namespc && namespc[0] ==
'\0') {
178 namespc = node->GetNamespace()?node->GetNamespace()->GetChars():NPT_XML_NO_NAMESPACE;
195 const char* namespc =
"",
196 NPT_Cardinal max_size = 1024) {
200 NPT_Result result = GetAttribute(node, name, attribute, namespc);
201 if (NPT_FAILED(result))
return result;
203 if (!attribute)
return NPT_FAILURE;
205 value = attribute->GetValue().SubString(0, max_size);
212 const char* namespc =
"") {
214 NPT_CHECK(GetAttribute(node, name, attribute, namespc));
215 if (!attribute)
return NPT_FAILURE;
217 attribute->SetValue(value);
224 const char* prefix = NULL) {
225 if (!node)
return NPT_FAILURE;
227 child->AddText(text);
228 return node->AddChild(child);
231 static bool IsMatch(
const NPT_XmlNode*
const & node,
const char* tag,
const char* namespc_mapped) {
238 if (element && element->GetTag() == tag) {
239 if (namespc_mapped) {
241 const NPT_String* namespc = element->GetNamespace();
245 return *namespc == namespc_mapped;
249 return namespc_mapped[0] ==
'\0';
262 const char* namespc =
"") {
263 if (!node)
return NPT_FAILURE;
266 if (namespc && namespc[0] ==
'\0') {
267 namespc = node->GetNamespace()?node->GetNamespace()->GetChars():NPT_XML_NO_NAMESPACE;
270 const char* namespc_mapped = (namespc==NULL)?
"":(namespc[0]==
'*' && namespc[1]==
'\0')?NULL:namespc;
278 if (IsMatch(*child, tag, namespc_mapped)) {
279 children.Add((*child)->AsElementNode());
288 const char* namespc =
"") {
289 if (!node)
return NULL;
292 if (namespc && namespc[0] ==
'\0') {
293 namespc = node->GetNamespace()?node->GetNamespace()->GetChars():NPT_XML_NO_NAMESPACE;
296 return node->GetChild(tag, namespc);
302 if (!parent)
return NPT_FAILURE;
310 if ((*children)->AsElementNode() && n-- == 0) {
311 child = (*children)->AsElementNode();
320 static NPT_Result Serialize(
NPT_XmlNode& node,
NPT_String& xml,
bool add_header =
true, NPT_Int8 indentation = 0) {
323 NPT_CHECK(writer.Serialize(node, *stream, add_header));
331 if (NPT_FAILED(writer.Serialize(node, *stream, add_header))) {
332 NPT_Debug(
"Failed to serialize xml node");
354 m_Value(value.GetChars()), m_IgnoreCase(ignore_case) {}
357 m_Value(value), m_IgnoreCase(ignore_case) {}
361 bool operator()(
const NPT_String*
const & value)
const {
362 return value->Compare(m_Value, m_IgnoreCase) ? false :
true;
364 bool operator()(
const NPT_String& value)
const {
365 return value.Compare(m_Value, m_IgnoreCase) ? false :
true;
389 return *value == m_Value;
392 return value == m_Value;
413 return message.GetHeaders().GetHeaderValue(
"ST");
417 return message.GetHeaders().SetHeader(
"ST", st);
421 return message.GetHeaders().GetHeaderValue(
"NT");
425 return message.GetHeaders().SetHeader(
"NT", nt);
429 return message.GetHeaders().GetHeaderValue(
"NTS");
433 return message.GetHeaders().SetHeader(
"NTS", nts);
437 return message.GetHeaders().GetHeaderValue(
"MAN");
441 return message.GetHeaders().SetHeader(
"MAN", man);
445 return message.GetHeaders().GetHeaderValue(
"Location");
448 const char* location) {
449 return message.GetHeaders().SetHeader(
"Location", location);
453 return message.GetHeaders().GetHeaderValue(NPT_HTTP_HEADER_SERVER);
457 bool replace =
true) {
458 return message.GetHeaders().SetHeader(
459 NPT_HTTP_HEADER_SERVER,
465 return message.GetHeaders().GetHeaderValue(
"USN");
469 return message.GetHeaders().SetHeader(
"USN", usn);
473 return message.GetHeaders().GetHeaderValue(
"CALLBACK");
475 static NPT_Result SetCallbacks(
NPT_HttpMessage& message,
const char* callbacks) {
476 return message.GetHeaders().SetHeader(
"CALLBACK", callbacks);
480 return message.GetHeaders().GetHeaderValue(
"SID");
484 return message.GetHeaders().SetHeader(
"SID", sid);
489 message.GetHeaders().GetHeaderValue(
"Cache-Control");
490 NPT_CHECK_POINTER(cc);
491 return ExtractLeaseTime(*cc, lease);
494 return message.GetHeaders().SetHeader(
"Cache-Control",
495 "max-age="+NPT_String::FromInteger(lease.ToSeconds()));
498 static NPT_Result GetBootId(
const NPT_HttpMessage& message, NPT_UInt32& bootId) {
500 const NPT_String* bid = message.GetHeaders().GetHeaderValue(
"BOOTID.UPNP.ORG");
501 NPT_CHECK_POINTER(bid);
502 return NPT_ParseInteger32(*bid, bootId,
false);
504 static NPT_Result SetBootId(
NPT_HttpMessage& message,
const NPT_UInt32& bootId) {
505 return message.GetHeaders().SetHeader(
"BOOTID.UPNP.ORG",
506 NPT_String::FromInteger(bootId));
509 static NPT_Result GetNextBootId(
const NPT_HttpMessage& message, NPT_UInt32& nextBootId) {
511 const NPT_String* nbid = message.GetHeaders().GetHeaderValue(
"NEXTBOOTID.UPNP.ORG");
512 NPT_CHECK_POINTER(nbid);
513 return NPT_ParseInteger32(*nbid, nextBootId,
false);
515 static NPT_Result SetNextBootId(
NPT_HttpMessage& message,
const NPT_UInt32& nextBootId) {
516 return message.GetHeaders().SetHeader(
"NEXTBOOTID.UPNP.ORG",
517 NPT_String::FromInteger(nextBootId));
520 static NPT_Result GetConfigId(
const NPT_HttpMessage& message, NPT_UInt32& configId) {
522 const NPT_String* cid = message.GetHeaders().GetHeaderValue(
"CONFIGID.UPNP.ORG");
523 NPT_CHECK_POINTER(cid);
524 return NPT_ParseInteger32(*cid, configId,
false);
526 static NPT_Result SetConfigId(
NPT_HttpMessage& message,
const NPT_UInt32& configId) {
527 return message.GetHeaders().SetHeader(
"CONFIGID.UPNP.ORG", NPT_String::FromInteger(configId));
530 static NPT_Result GetTimeOut(
const NPT_HttpMessage& message, NPT_Int32& seconds) {
533 message.GetHeaders().GetHeaderValue(
"TIMEOUT");
534 NPT_CHECK_POINTER(timeout);
535 return ExtractTimeOut(*timeout, seconds);
537 static NPT_Result SetTimeOut(
NPT_HttpMessage& message,
const NPT_Int32 seconds) {
539 return message.GetHeaders().SetHeader(
"TIMEOUT",
"Second-"+NPT_String::FromInteger(seconds));
541 return message.GetHeaders().SetHeader(
"TIMEOUT",
"Second-infinite");
547 NPT_System::GetCurrentTimeStamp(now);
550 return message.GetHeaders().SetHeader(
"Date", date.ToString(NPT_DateTime::FORMAT_RFC_1123));
554 const NPT_String* value = message.GetHeaders().GetHeaderValue(
"If-Modified-Since");
555 if (!value)
return NPT_FAILURE;
558 if (NPT_SUCCEEDED(date.FromString(*value, NPT_DateTime::FORMAT_RFC_1123)))
561 if (NPT_SUCCEEDED(date.FromString(*value, NPT_DateTime::FORMAT_RFC_1036)))
564 return date.FromString(*value, NPT_DateTime::FORMAT_ANSI);
567 return message.GetHeaders().SetHeader(
"If-Modified-Since",
568 date.ToString(NPT_DateTime::FORMAT_RFC_1123));
571 static NPT_Result GetMX(
const NPT_HttpMessage& message, NPT_UInt32& value) {
574 message.GetHeaders().GetHeaderValue(
"MX");
575 NPT_CHECK_POINTER(mx);
576 return NPT_ParseInteger32(*mx, value,
false);
578 static NPT_Result SetMX(
NPT_HttpMessage& message,
const NPT_UInt32 mx) {
579 return message.GetHeaders().SetHeader(
"MX",
580 NPT_String::FromInteger(mx));
583 static NPT_Result GetSeq(
const NPT_HttpMessage& message, NPT_UInt32& value) {
586 message.GetHeaders().GetHeaderValue(
"SEQ");
587 NPT_CHECK_POINTER(seq);
588 return NPT_ParseInteger32(*seq, value);
590 static NPT_Result SetSeq(
NPT_HttpMessage& message,
const NPT_UInt32 seq) {
591 return message.GetHeaders().SetHeader(
"SEQ",
592 NPT_String::FromInteger(seq));
595 static const char* GenerateUUID(
int count,
NPT_String& uuid) {
597 for (
int i=0;i<(count<100?count:100);i++) {
598 int random = NPT_System::GetRandomInteger();
599 uuid += (char)((random % 25) + 66);
604 static const char* GenerateSerialNumber(
NPT_String& sn,
int count = 40) {
606 for (
int i=0;i<count;i++) {
607 char nibble = (char)(NPT_System::GetRandomInteger() % 16);
608 sn += (nibble < 10) ? (
'0' + nibble) : (
'a' + (nibble-10));
614 static const char* GenerateGUID(
NPT_String& guid) {
616 for (
int i=0;i<32;i++) {
617 char nibble = (char)(NPT_System::GetRandomInteger() % 16);
618 guid += (nibble < 10) ? (
'0' + nibble) : (
'a' + (nibble-10));
619 if (i == 7 || i == 11 || i == 15 || i == 19) {
628 if (cache_control.StartsWith(
"max-age=",
true) &&
629 NPT_SUCCEEDED(NPT_ParseInteger32(cache_control.GetChars()+8, value))) {
630 lease.SetSeconds(value);
636 static NPT_Result ExtractTimeOut(
const char* timeout, NPT_Int32& len) {
638 if (temp.CompareN(
"Second-", 7,
true)) {
639 return NPT_ERROR_INVALID_FORMAT;
642 if (temp.Compare(
"Second-infinite",
true) == 0) {
643 len = NPT_TIMEOUT_INFINITE;
646 return temp.SubString(7).ToInteger(len);
651 NPT_CHECK(GetNetworkInterfaces(if_list, with_localhost));
655 NPT_IpAddress ip = (*(*iface)->GetAddresses().GetFirstItem()).GetPrimaryAddress();
656 if (ip.ToString().Compare(
"0.0.0.0") &&
657 (with_localhost || ip.ToString().Compare(
"127.0.0.1"))) {
665 localhost.Parse(
"127.0.0.1");
674 bool with_localhost =
false) {
675 NPT_CHECK(_GetNetworkInterfaces(if_list, with_localhost,
false));
678 if (if_list.GetItemCount() == 0) {
679 NPT_CHECK(_GetNetworkInterfaces(if_list,
true,
true));
686 NPT_CHECK(GetNetworkInterfaces(if_list));
690 NPT_String ip = (*(*iface)->GetAddresses().GetFirstItem()).GetPrimaryAddress().ToString();
691 if (ip.Compare(
"0.0.0.0") && ip.Compare(
"127.0.0.1")) {
692 addresses.Add((*iface)->GetMacAddress().ToString());
702 static bool IsLocalNetworkAddress(
const NPT_IpAddress& address) {
703 if (address.ToString() ==
"127.0.0.1")
return true;
706 NPT_NetworkInterface::GetNetworkInterfaces(if_list);
710 if((*iface)->IsAddressInNetwork(address))
return true;
721 bool include_localhost =
false,
722 bool only_localhost =
false) {
724 NPT_CHECK(NPT_NetworkInterface::GetNetworkInterfaces(_if_list));
727 while (NPT_SUCCEEDED(_if_list.PopHead(iface))) {
729 if ((iface->GetAddresses().GetItemCount() == 0) ||
730 !(iface->GetFlags() & NPT_NETWORK_INTERFACE_FLAG_MULTICAST) ||
731 (iface->GetFlags() & NPT_NETWORK_INTERFACE_FLAG_POINT_TO_POINT)) {
736 NPT_String ip = iface->GetAddresses().GetFirstItem()->GetPrimaryAddress().ToString();
738 if (iface->GetFlags() & NPT_NETWORK_INTERFACE_FLAG_LOOPBACK) {
739 if (include_localhost || only_localhost) {
743 }
else if (ip.Compare(
"0.0.0.0") && !only_localhost) {
757 #endif // _PLT_UTILITIES_H_ Definition: NptNetwork.h:216
Definition: NptArray.h:54
Definition: NptHttp.h:241
The PLT_XmlAttributeFinder class is used to determine if an attribute exists given an xml element nod...
Definition: PltUtilities.h:50
Definition: NptNetwork.h:74
Definition: NptStreams.h:251
The NPT_IpAddressFinder class is used to determine if a IP Address is found as part of a list of IP A...
Definition: PltUtilities.h:381
The PLT_XmlHelper class is a set of utility functions for manipulating xml documents and DOM trees...
Definition: PltUtilities.h:96
The NPT_StringFinder class is used to determine if a string is found as part of a list of strings...
Definition: PltUtilities.h:349
The PLT_UPnPMessageHelper class is a set of utility functions for manipulating specific UPnP HTTP hea...
Definition: PltUtilities.h:408
Definition: NptReferences.h:45
Definition: NptTime.h:116
Definition: NptStrings.h:57
Definition: NptCommon.h:45