35 #ifndef _PLT_UTILITIES_H_
36 #define _PLT_UTILITIES_H_
58 const char* namespc) :
59 m_Element(element), m_Name(name), m_Namespace(namespc) {}
61 bool operator()(
const NPT_XmlAttribute*
const & attribute)
const {
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;
84 const NPT_XmlElementNode& m_Element;
86 const char* m_Namespace;
102 static NPT_Result Parse(
const NPT_String& xml,
103 NPT_XmlElementNode*& tree) {
108 NPT_XmlParser parser;
110 NPT_Result result = parser.Parse(xml, node);
111 if (NPT_FAILED(result)) {
116 tree = node->AsElementNode();
125 static NPT_Result GetChildText(NPT_XmlElementNode* node,
128 const char* namespc =
"",
129 NPT_Cardinal max_size = 1024) {
132 if (!node)
return NPT_FAILURE;
135 if (namespc && namespc[0] ==
'\0') namespc = node->GetNamespace()?node->GetNamespace()->GetChars():NPT_XML_NO_NAMESPACE;
137 NPT_XmlElementNode* child = node->GetChild(tag, namespc);
138 if (!child)
return NPT_FAILURE;
140 const NPT_String* text = child->GetText();
142 value = text?text->SubString(0, max_size):
"";
146 static NPT_Result RemoveAttribute(NPT_XmlElementNode* node,
148 const char* namespc =
"") {
149 if (!node)
return NPT_FAILURE;
152 if (namespc && namespc[0] ==
'\0') namespc = node->GetNamespace()?node->GetNamespace()->GetChars():NPT_XML_NO_NAMESPACE;
154 NPT_List<NPT_XmlAttribute*>::Iterator attribute;
156 if (!attribute)
return NPT_FAILURE;
159 NPT_CHECK(node->GetAttributes().Erase(attribute));
164 static NPT_Result GetAttribute(NPT_XmlElementNode* node,
166 NPT_XmlAttribute*& attr,
167 const char* namespc =
"") {
170 if (!node)
return NPT_FAILURE;
173 if (namespc && namespc[0] ==
'\0') namespc = node->GetNamespace()?node->GetNamespace()->GetChars():NPT_XML_NO_NAMESPACE;
175 NPT_List<NPT_XmlAttribute*>::Iterator attribute;
186 static NPT_Result GetAttribute(NPT_XmlElementNode* node,
189 const char* namespc =
"",
190 NPT_Cardinal max_size = 1024) {
193 NPT_XmlAttribute* attribute = NULL;
194 NPT_Result result = GetAttribute(node, name, attribute, namespc);
195 if (NPT_FAILED(result))
return result;
197 if (!attribute)
return NPT_FAILURE;
199 value = attribute->GetValue().SubString(0, max_size);
203 static NPT_Result SetAttribute(NPT_XmlElementNode* node,
206 const char* namespc =
"") {
207 NPT_XmlAttribute* attribute = NULL;
208 NPT_CHECK(GetAttribute(node, name, attribute, namespc));
209 if (!attribute)
return NPT_FAILURE;
211 attribute->SetValue(value);
215 static NPT_Result AddChildText(NPT_XmlElementNode* node,
218 const char* prefix = NULL) {
219 if (!node)
return NPT_FAILURE;
220 NPT_XmlElementNode* child =
new NPT_XmlElementNode(prefix, tag);
221 child->AddText(text);
222 return node->AddChild(child);
225 static bool IsMatch(
const NPT_XmlNode*
const & node,
const char* tag,
const char* namespc_mapped) {
230 const NPT_XmlElementNode* element = node->AsElementNode();
232 if (element && element->GetTag() == tag) {
233 if (namespc_mapped) {
235 const NPT_String* namespc = element->GetNamespace();
239 return *namespc == namespc_mapped;
243 return namespc_mapped[0] ==
'\0';
253 static NPT_Result GetChildren(NPT_XmlElementNode* node,
254 NPT_Array<NPT_XmlElementNode*>& children,
256 const char* namespc =
"") {
257 if (!node)
return NPT_FAILURE;
260 if (namespc && namespc[0] ==
'\0') namespc = node->GetNamespace()?node->GetNamespace()->GetChars():NPT_XML_NO_NAMESPACE;
262 const char* namespc_mapped = (namespc==NULL)?
"":(namespc[0]==
'*' && namespc[1]==
'\0')?NULL:namespc;
265 NPT_List<NPT_XmlNode*>& allchildren = node->GetChildren();
268 NPT_List<NPT_XmlNode*>::Iterator child = allchildren.GetFirstItem();
270 if (IsMatch(*child, tag, namespc_mapped)) {
271 children.Add((*child)->AsElementNode());
278 static NPT_XmlElementNode* GetChild(NPT_XmlElementNode* node,
280 const char* namespc =
"") {
281 if (!node)
return NULL;
284 if (namespc && namespc[0] ==
'\0') namespc = node->GetNamespace()?node->GetNamespace()->GetChars():NPT_XML_NO_NAMESPACE;
286 return node->GetChild(tag, namespc);
289 static NPT_Result GetChild(NPT_XmlElementNode* parent,
290 NPT_XmlElementNode*& child,
292 if (!parent)
return NPT_FAILURE;
298 NPT_List<NPT_XmlNode*>::Iterator children = parent->GetChildren().GetFirstItem();
300 if ((*children)->AsElementNode() && n-- == 0) {
301 child = (*children)->AsElementNode();
310 static NPT_Result Serialize(NPT_XmlNode& node, NPT_String& xml,
bool add_header =
true, NPT_Int8 indentation = 0) {
311 NPT_XmlWriter writer(indentation);
312 NPT_StringOutputStreamReference stream(
new NPT_StringOutputStream(&xml));
313 NPT_CHECK(writer.Serialize(node, *stream, add_header));
317 static NPT_String Serialize(NPT_XmlNode& node,
bool add_header =
true, NPT_Int8 indentation = 0) {
318 NPT_XmlWriter writer(indentation);
320 NPT_StringOutputStreamReference stream(
new NPT_StringOutputStream(&xml));
321 if (NPT_FAILED(writer.Serialize(node, *stream, add_header))) {
322 NPT_Debug(
"Failed to serialize xml node");
344 m_Value(value), m_IgnoreCase(ignore_case) {}
346 bool operator()(
const NPT_String*
const & value)
const {
347 return value->Compare(m_Value, m_IgnoreCase) ?
false :
true;
349 bool operator()(
const NPT_String& value)
const {
350 return value.Compare(m_Value, m_IgnoreCase) ?
false :
true;
373 bool operator()(
const NPT_IpAddress*
const & value)
const {
374 return *value == m_Value;
376 bool operator()(
const NPT_IpAddress& value)
const {
377 return value == m_Value;
382 NPT_IpAddress m_Value;
397 static const NPT_String* GetST(
const NPT_HttpMessage& message) {
398 return message.GetHeaders().GetHeaderValue(
"ST");
400 static NPT_Result SetST(NPT_HttpMessage& message,
402 return message.GetHeaders().SetHeader(
"ST", st);
404 static const NPT_String* GetNT(
const NPT_HttpMessage& message) {
405 return message.GetHeaders().GetHeaderValue(
"NT");
407 static NPT_Result SetNT(NPT_HttpMessage& message,
409 return message.GetHeaders().SetHeader(
"NT", nt);
411 static const NPT_String* GetNTS(
const NPT_HttpMessage& message) {
412 return message.GetHeaders().GetHeaderValue(
"NTS");
414 static NPT_Result SetNTS(NPT_HttpMessage& message,
416 return message.GetHeaders().SetHeader(
"NTS", nts);
418 static const NPT_String* GetMAN(
const NPT_HttpMessage& message) {
419 return message.GetHeaders().GetHeaderValue(
"MAN");
421 static NPT_Result SetMAN(NPT_HttpMessage& message,
423 return message.GetHeaders().SetHeader(
"MAN", man);
425 static const NPT_String* GetLocation(
const NPT_HttpMessage& message) {
426 return message.GetHeaders().GetHeaderValue(
"Location");
428 static NPT_Result SetLocation(NPT_HttpMessage& message,
429 const char* location) {
430 return message.GetHeaders().SetHeader(
"Location", location);
432 static const NPT_String* GetServer(
const NPT_HttpMessage& message) {
433 return message.GetHeaders().GetHeaderValue(NPT_HTTP_HEADER_SERVER);
435 static NPT_Result SetServer(NPT_HttpMessage& message,
437 bool replace =
true) {
438 return message.GetHeaders().SetHeader(
439 NPT_HTTP_HEADER_SERVER,
443 static const NPT_String* GetUSN(
const NPT_HttpMessage& message) {
444 return message.GetHeaders().GetHeaderValue(
"USN");
446 static NPT_Result SetUSN(NPT_HttpMessage& message,
448 return message.GetHeaders().SetHeader(
"USN", usn);
450 static const NPT_String* GetCallbacks(
const NPT_HttpMessage& message) {
451 return message.GetHeaders().GetHeaderValue(
"CALLBACK");
453 static NPT_Result SetCallbacks(NPT_HttpMessage& message,
454 const char* callbacks) {
455 return message.GetHeaders().SetHeader(
"CALLBACK", callbacks);
457 static const NPT_String* GetSID(
const NPT_HttpMessage& message) {
458 return message.GetHeaders().GetHeaderValue(
"SID");
460 static NPT_Result SetSID(NPT_HttpMessage& message,
462 return message.GetHeaders().SetHeader(
"SID", sid);
464 static NPT_Result GetLeaseTime(
const NPT_HttpMessage& message,
465 NPT_TimeInterval& lease) {
466 const NPT_String* cc =
467 message.GetHeaders().GetHeaderValue(
"Cache-Control");
468 NPT_CHECK_POINTER(cc);
469 return ExtractLeaseTime(*cc, lease);
471 static NPT_Result SetLeaseTime(NPT_HttpMessage& message,
472 const NPT_TimeInterval& lease) {
473 return message.GetHeaders().SetHeader(
475 "max-age="+NPT_String::FromInteger(lease.ToSeconds()));
477 static NPT_Result GetTimeOut(
const NPT_HttpMessage& message,
478 NPT_Int32& seconds) {
480 const NPT_String* timeout =
481 message.GetHeaders().GetHeaderValue(
"TIMEOUT");
482 NPT_CHECK_POINTER(timeout);
483 return ExtractTimeOut(*timeout, seconds);
485 static NPT_Result SetTimeOut(NPT_HttpMessage& message,
486 const NPT_Int32 seconds) {
488 return message.GetHeaders().SetHeader(
490 "Second-"+NPT_String::FromInteger(seconds));
492 return message.GetHeaders().SetHeader(
497 static NPT_Result SetDate(NPT_HttpMessage& message) {
499 NPT_System::GetCurrentTimeStamp(now);
500 NPT_DateTime date(now);
502 return message.GetHeaders().SetHeader(
"Date", date.ToString(NPT_DateTime::FORMAT_RFC_1123));
504 static NPT_Result GetIfModifiedSince(
const NPT_HttpMessage& message,
505 NPT_DateTime& date) {
507 const NPT_String* value =
508 message.GetHeaders().GetHeaderValue(
"If-Modified-Since");
509 if (!value)
return NPT_FAILURE;
512 if (NPT_SUCCEEDED(date.FromString(*value, NPT_DateTime::FORMAT_RFC_1123)))
514 if (NPT_SUCCEEDED(date.FromString(*value, NPT_DateTime::FORMAT_RFC_1036)))
516 return date.FromString(*value, NPT_DateTime::FORMAT_ANSI);
518 static NPT_Result SetIfModifiedSince(NPT_HttpMessage& message,
519 const NPT_DateTime& date) {
520 return message.GetHeaders().SetHeader(
522 date.ToString(NPT_DateTime::FORMAT_RFC_1123));
524 static NPT_Result GetMX(
const NPT_HttpMessage& message,
527 const NPT_String* mx =
528 message.GetHeaders().GetHeaderValue(
"MX");
529 NPT_CHECK_POINTER(mx);
530 return NPT_ParseInteger32(*mx, value,
false);
532 static NPT_Result SetMX(NPT_HttpMessage& message,
533 const NPT_UInt32 mx) {
534 return message.GetHeaders().SetHeader(
536 NPT_String::FromInteger(mx));
538 static NPT_Result GetSeq(
const NPT_HttpMessage& message,
541 const NPT_String* seq =
542 message.GetHeaders().GetHeaderValue(
"SEQ");
543 NPT_CHECK_POINTER(seq);
544 return NPT_ParseInteger32(*seq, value);
546 static NPT_Result SetSeq(NPT_HttpMessage& message,
547 const NPT_UInt32 seq) {
548 return message.GetHeaders().SetHeader(
550 NPT_String::FromInteger(seq));
552 static const char* GenerateUUID(
int count,
555 for (
int i=0;i<(count<100?count:100);i++) {
556 int random = NPT_System::GetRandomInteger();
557 uuid += (char)((random % 25) + 66);
562 static const char* GenerateSerialNumber(NPT_String& sn,
int count = 40) {
564 for (
int i=0;i<count;i++) {
565 char nibble = (char)(NPT_System::GetRandomInteger() % 16);
566 sn += (nibble < 10) ? (
'0' + nibble) : (
'a' + (nibble-10));
571 static const char* GenerateGUID(NPT_String& guid) {
573 for (
int i=0;i<32;i++) {
574 char nibble = (char)(NPT_System::GetRandomInteger() % 16);
575 guid += (nibble < 10) ? (
'0' + nibble) : (
'a' + (nibble-10));
576 if (i == 7 || i == 11 || i == 15 || i == 19) {
582 static NPT_Result ExtractLeaseTime(
const NPT_String& cache_control,
583 NPT_TimeInterval& lease) {
585 if (cache_control.StartsWith(
"max-age=",
true) &&
586 NPT_SUCCEEDED(NPT_ParseInteger32(cache_control.GetChars()+8,
588 lease.SetSeconds(value);
593 static NPT_Result ExtractTimeOut(
const char* timeout,
595 NPT_String temp = timeout;
596 if (temp.CompareN(
"Second-", 7,
true)) {
597 return NPT_ERROR_INVALID_FORMAT;
600 if (temp.Compare(
"Second-infinite",
true) == 0) {
601 len = NPT_TIMEOUT_INFINITE;
604 return temp.SubString(7).ToInteger(len);
606 static NPT_Result GetIPAddresses(NPT_List<NPT_IpAddress>& ips,
607 bool with_localhost =
false) {
608 NPT_List<NPT_NetworkInterface*> if_list;
609 NPT_CHECK(GetNetworkInterfaces(if_list, with_localhost));
611 NPT_List<NPT_NetworkInterface*>::Iterator iface = if_list.GetFirstItem();
613 NPT_IpAddress ip = (*(*iface)->GetAddresses().GetFirstItem()).GetPrimaryAddress();
614 if (ip.ToString().Compare(
"0.0.0.0") &&
615 (with_localhost || ip.ToString().Compare(
"127.0.0.1"))) {
622 NPT_IpAddress localhost;
623 localhost.Parse(
"127.0.0.1");
627 if_list.Apply(NPT_ObjectDeleter<NPT_NetworkInterface>());
631 static NPT_Result GetNetworkInterfaces(NPT_List<NPT_NetworkInterface*>& if_list,
632 bool with_localhost =
false) {
633 NPT_CHECK(_GetNetworkInterfaces(if_list, with_localhost,
false));
636 if (if_list.GetItemCount() == 0) {
637 NPT_CHECK(_GetNetworkInterfaces(if_list,
true,
true));
642 static NPT_Result GetMACAddresses(NPT_List<NPT_String>& addresses) {
643 NPT_List<NPT_NetworkInterface*> if_list;
644 NPT_CHECK(GetNetworkInterfaces(if_list));
646 NPT_List<NPT_NetworkInterface*>::Iterator iface = if_list.GetFirstItem();
648 NPT_String ip = (*(*iface)->GetAddresses().GetFirstItem()).GetPrimaryAddress().ToString();
649 if (ip.Compare(
"0.0.0.0") && ip.Compare(
"127.0.0.1")) {
650 addresses.Add((*iface)->GetMacAddress().ToString());
655 if_list.Apply(NPT_ObjectDeleter<NPT_NetworkInterface>());
660 static bool IsLocalNetworkAddress(
const NPT_IpAddress& address) {
661 if (address.ToString() ==
"127.0.0.1")
return true;
663 NPT_List<NPT_NetworkInterface*> if_list;
664 NPT_NetworkInterface::GetNetworkInterfaces(if_list);
666 NPT_List<NPT_NetworkInterface*>::Iterator iface = if_list.GetFirstItem();
668 if((*iface)->IsAddressInNetwork(address))
return true;
672 if_list.Apply(NPT_ObjectDeleter<NPT_NetworkInterface>());
678 static NPT_Result _GetNetworkInterfaces(NPT_List<NPT_NetworkInterface*>& if_list,
679 bool include_localhost =
false,
680 bool only_localhost =
false) {
681 NPT_List<NPT_NetworkInterface*> _if_list;
682 NPT_CHECK(NPT_NetworkInterface::GetNetworkInterfaces(_if_list));
684 NPT_NetworkInterface* iface;
685 while (NPT_SUCCEEDED(_if_list.PopHead(iface))) {
687 if ((iface->GetAddresses().GetItemCount() == 0) ||
688 !(iface->GetFlags() & NPT_NETWORK_INTERFACE_FLAG_MULTICAST) ||
689 (iface->GetFlags() & NPT_NETWORK_INTERFACE_FLAG_POINT_TO_POINT)) {
694 NPT_String ip = iface->GetAddresses().GetFirstItem()->GetPrimaryAddress().ToString();
696 if (iface->GetFlags() & NPT_NETWORK_INTERFACE_FLAG_LOOPBACK) {
697 if (include_localhost || only_localhost) {
701 }
else if (ip.Compare(
"0.0.0.0") && !only_localhost) {
710 _if_list.Apply(NPT_ObjectDeleter<NPT_NetworkInterface>());
715 #endif // _PLT_UTILITIES_H_