-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwurfl.go
1233 lines (1052 loc) · 37.7 KB
/
wurfl.go
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package wurfl - WURFL InFuze Golang module
// wurfl is a golang package wrapping the WURFL C API and encapsulating it in
// 2 golang types to provide a fast and intuitive interface.
// It is released for linux/macos platforms.
package wurfl
//
//#cgo darwin CFLAGS: -I/usr/local/include
//#cgo darwin LDFLAGS: -L/usr/local/lib/
//#cgo windows CFLAGS: -I"C:/Program Files/Scientiamobile/InFuze/dev/include"
//#cgo windows LDFLAGS: -L"C:/Program Files/Scientiamobile/InFuze/bin"
//#cgo LDFLAGS: -lwurfl
//#include <stdlib.h>
//#include <wurfl/wurfl.h>
//#include <stdio.h>
import "C"
import (
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"unsafe"
)
// Engine Target possible values
// DEPRECATED as of 1.9.5.0
const (
WurflEngineTargetHighAccuray = C.WURFL_ENGINE_TARGET_HIGH_ACCURACY
WurflEngineTargetHighPerformance = C.WURFL_ENGINE_TARGET_HIGH_PERFORMANCE
WurflEngineTargetDefault = C.WURFL_ENGINE_TARGET_DEFAULT
WurflEngineTargetFastDesktopBrowserMatch = C.WURFL_ENGINE_TARGET_FAST_DESKTOP_BROWSER_MATCH
)
// UserAgent priority possible values
// DEPRECATED as of 1.9.5.0
const (
WurflUserAgentPriorityOverrideSideloadedBrowserUserAgent = C.WURFL_USERAGENT_PRIORITY_OVERRIDE_SIDELOADED_BROWSER_USERAGENT
WurflUserAgentPriorityUsePlainUserAgent = C.WURFL_USERAGENT_PRIORITY_USE_PLAIN_USERAGENT
)
const (
// WurflAttrExtraHeadersExperimental is deprecated since 1.12.5.0 and should not be used.
WurflAttrExtraHeadersExperimental = C.WURFL_ATTR_EXTRA_HEADERS_EXPERIMENTAL
// WurflAttrCapabilityFallbackCache attribute controls the capability fallback cache (needs libwurfl version 1.12.9.3 or above)
WurflAttrCapabilityFallbackCache = C.WURFL_ATTR_CAPABILITY_FALLBACK_CACHE
)
// To control capability fallback cache, needs libwurfl version 1.12.9.3 or above
const (
// WurflAttrCapabilityFallbackCacheDefault is the default setting for capability fallback cache
WurflAttrCapabilityFallbackCacheDefault = C.WURFL_ATTR_CAPABILITY_FALLBACK_CACHE_DEFAULT
// WurflAttrCapabilityFallbackCacheDisabled disables the capability fallback cache
WurflAttrCapabilityFallbackCacheDisabled = C.WURFL_ATTR_CAPABILITY_FALLBACK_CACHE_DISABLED
// WurflAttrCapabilityFallbackCacheLimited sets the capability fallback cache to limited mode
WurflAttrCapabilityFallbackCacheLimited = C.WURFL_ATTR_CAPABILITY_FALLBACK_CACHE_LIMITED
)
// Cache Provider possible values
const (
WurflCacheProviderDefault = -1
WurflCacheProviderNone = C.WURFL_CACHE_PROVIDER_NONE
WurflCacheProviderLru = C.WURFL_CACHE_PROVIDER_LRU
// Deprecated: use WurflCacheProviderLru instead
WurflCacheProviderDoubleLru = C.WURFL_CACHE_PROVIDER_DOUBLE_LRU
)
// Match type
const (
WurflMatchTypeExact = C.WURFL_MATCH_TYPE_EXACT
WurflMatchTypeConclusive = C.WURFL_MATCH_TYPE_CONCLUSIVE
WurflMatchTypeRecovery = C.WURFL_MATCH_TYPE_RECOVERY
WurflMatchTypeCatchall = C.WURFL_MATCH_TYPE_CATCHALL
WurflMatchTypeHighPerformance = C.WURFL_MATCH_TYPE_HIGHPERFORMANCE
WurflMatchTypeNone = C.WURFL_MATCH_TYPE_NONE
WurflMatchTypeCached = C.WURFL_MATCH_TYPE_CACHED
)
// Wurfl enumerator type
const (
WurflEnumStaticCapabilities = C.WURFL_ENUM_STATIC_CAPABILITIES
WurflEnumVirtualCapabilities = C.WURFL_ENUM_VIRTUAL_CAPABILITIES
WurflEnumMandatoryCapabilities = C.WURFL_ENUM_MANDATORY_CAPABILITIES
WurflEnumWurflID = C.WURFL_ENUM_WURFLID
)
// Wurfl updater frequency
const (
WurflUpdaterFrequencyDaily = C.WURFL_UPDATER_FREQ_DAILY
WurflUpdaterFrequencyWeekly = C.WURFL_UPDATER_FREQ_WEEKLY
)
// HeaderQuality represents the header quality value
type HeaderQuality int
func (hq HeaderQuality) String() string {
switch hq {
case HeaderQualityNone:
return "None"
case HeaderQualityBasic:
return "Basic"
case HeaderQualityFull:
return "Full"
}
return "Unknown"
}
const (
// HeaderQualityNone no User Agent Client Hints are present.
HeaderQualityNone HeaderQuality = C.WURFL_ENUM_UACH_NONE
// HeaderQualityBasic only some of the headers needed for a successful WURFL detection are present.
HeaderQualityBasic HeaderQuality = C.WURFL_ENUM_UACH_BASIC
// HeaderQualityFull all the headers needed for a successful WURFL detection are present.
HeaderQualityFull HeaderQuality = C.WURFL_ENUM_UACH_FULL
)
// Wurfl represents internal wurfl infuze handle
type Wurfl struct {
Wurfl C.wurfl_handle
ImportantHeaderNames []string
importantHeaderCStringNames []*C.char
capsCStringcache map[string]*C.char
}
// Device represent internal matched device handle
type Device struct {
Device C.wurfl_device_handle
Wurfl C.wurfl_handle
capsCStringcache map[string]*C.char
}
// WurflHandler defines API methods for the Wurfl Infuze handle
type WurflHandler interface {
GetAPIVersion() string
SetLogPath(LogFile string) error
IsUserAgentFrozen(ua string) bool
LookupDeviceIDWithImportantHeaderMap(DeviceID string, IHMap map[string]string) (DeviceHandler, error)
LookupWithImportantHeaderMap(IHMap map[string]string) (DeviceHandler, error)
LookupDeviceIDWithRequest(DeviceID string, r *http.Request) (DeviceHandler, error)
LookupRequest(r *http.Request) (DeviceHandler, error)
LookupUserAgent(ua string) (DeviceHandler, error)
GetAllDeviceIds() []string
LookupDeviceID(DeviceID string) (DeviceHandler, error)
Destroy()
GetAllVCaps() []string
GetAllCaps() []string
GetInfo() string
GetHeaderQuality(r *http.Request) (HeaderQuality, error)
GetLastLoadTime() string
HasCapability(cap string) bool
HasVirtualCapability(vcap string) bool
SetAttr(attr int, value int) error
GetAttr(attr int) (int, error)
GetLastUpdated() string
}
// DeviceHandler defines API methods for the Wurfl Device handle
type DeviceHandler interface {
GetMatchType() int
GetVirtualCapabilities(caps []string) map[string]string
GetVirtualCaps(caps []string) (map[string]string, error)
GetVirtualCapability(vcap string) string
GetVirtualCap(vcap string) (string, error)
GetVirtualCapabilityAsInt(vcsp string) (int, error)
GetCapabilities(caps []string) map[string]string
GetStaticCaps(caps []string) (map[string]string, error)
GetCapability(cap string) string
GetStaticCap(cap string) (string, error)
GetCapabilityAsInt(cap string) (int, error)
IsRoot() bool
GetRootID() string
GetParentID() string
GetDeviceID() (string, error)
GetNormalizedUserAgent() (string, error)
GetOriginalUserAgent() (string, error)
GetUserAgent() (string, error)
Destroy()
}
// Updater defines API methods for the Updater
type Updater interface {
SetUpdaterDataURL(DataURL string) error
SetUpdaterDataFrequency(Frequency int) error
SetUpdaterDataURLTimeout(ConnectionTimeout int, DataTransferTimeout int) error
SetUpdaterLogPath(LogFile string) error
UpdaterRunonce() error
UpdaterStart() error
UpdaterStop() error
SetUpdaterUserAgent(userAgent string) error
GetUpdaterUserAgent() string
}
// Version is the current version of this package.
const Version = "1.30.4"
// APIVersion returns version of internal InFuze API without an initialized engine
func APIVersion() string {
return C.GoString(C.wurfl_get_api_version())
}
// Download downloads the WURFL data file from the specified URL and saves it to the specified folder.
// If the download is successful, it returns nil. Otherwise, it returns an error.
func Download(url string, folder string) error {
cURL := C.CString(url)
cFolder := C.CString(folder)
defer C.free(unsafe.Pointer(cURL))
defer C.free(unsafe.Pointer(cFolder))
cerr := C.wurfl_download(cURL, cFolder)
if cerr != C.WURFL_OK {
errMsg := C.GoString(C.wurfl_get_error_string(cerr))
return fmt.Errorf("WurflDownload failed: %s", errMsg)
}
return nil
}
// Create the wurfl engine. Parameters :
// Wurflxml : path to the wurfl.xml/zip file
// Patches : slice of paths of patches files to load
// CapFilter : list of capabilities used; allow to init engine without loading all 500+ caps
//
// Note : Capability filtering is discouraged and will be deprecated in future versions
//
// DEPRECATED: EngineTarget : As of 1.9.5.0 has no effect anymore
// CacheProvider : WurflCacheProviderLru
// CacheExtraConfig : size of single lru cache in the form "100000"
func Create(Wurflxml string, Patches []string, CapFilter []string, EngineTarget int, CacheProvider int, CacheExtraConfig string) (*Wurfl, error) {
w := &Wurfl{}
w.Wurfl = C.wurfl_create()
if w.Wurfl == nil {
// error in create
err := C.wurfl_get_error_message(w.Wurfl)
return nil, errors.New(C.GoString(err))
}
// setting cache if specified
if CacheProvider != WurflCacheProviderDefault {
ccacheec := C.CString(CacheExtraConfig)
cp := C.wurfl_cache_provider(CacheProvider)
C.wurfl_set_cache_provider(w.Wurfl, cp, ccacheec)
C.free(unsafe.Pointer(ccacheec))
}
// setting wurfl.xml
wxml := C.CString(Wurflxml)
C.wurfl_set_root(w.Wurfl, wxml)
C.free(unsafe.Pointer(wxml))
// setting patches
for i := 0; i < len(Patches); i++ {
cpatch := C.CString(Patches[i])
C.wurfl_add_patch(w.Wurfl, cpatch)
C.free(unsafe.Pointer(cpatch))
}
// filter capabilities in engine
for i := 0; i < len(CapFilter); i++ {
ccap := C.CString(CapFilter[i])
C.wurfl_add_requested_capability(w.Wurfl, ccap)
C.free(unsafe.Pointer(ccap))
}
// loading engine
if C.wurfl_load(w.Wurfl) != C.WURFL_OK {
err := C.wurfl_get_error_message(w.Wurfl)
return nil, errors.New(C.GoString(err))
}
// prepare important headers slice
ihe := C.wurfl_get_important_header_enumerator(w.Wurfl)
for i := 0; C.wurfl_important_header_enumerator_is_valid(ihe) != 0; i++ {
// get the header name
headerName := C.wurfl_important_header_enumerator_get_value(ihe)
// convert header name to go string
gheaderName := C.GoString(headerName)
// create a C string copy from the go string
cheaderName := C.CString(gheaderName)
// append to slice
w.ImportantHeaderNames = append(w.ImportantHeaderNames, gheaderName)
w.importantHeaderCStringNames = append(w.importantHeaderCStringNames, cheaderName)
// advance
C.wurfl_important_header_enumerator_move_next(ihe)
}
C.wurfl_important_header_enumerator_destroy(ihe)
// initialize caps/vcaps CString cache for faster calls to libwurfl
caps := w.GetAllCaps()
vcaps := w.GetAllCaps()
w.capsCStringcache = make(map[string]*C.char, len(caps)+len(vcaps))
for c := range caps {
w.capsCStringcache[caps[c]] = C.CString(caps[c])
}
for v := range vcaps {
w.capsCStringcache[vcaps[v]] = C.CString(vcaps[v])
}
return w, nil
}
// Destroy the wurfl engine
func (w *Wurfl) Destroy() {
if w.Wurfl != nil {
// deallocate important headers C strings
for _, importantHeaderName := range w.importantHeaderCStringNames {
C.free(unsafe.Pointer(importantHeaderName))
}
// now free the caps/vcaps CStrings cache
for _, v := range w.capsCStringcache {
C.free(unsafe.Pointer(v))
}
C.wurfl_destroy(w.Wurfl)
w.Wurfl = nil
}
}
// SetAttr : set engine attributes
func (w *Wurfl) SetAttr(attr int, value int) error {
cattr := C.wurfl_attr(attr)
cvalue := C.int(value)
if C.wurfl_set_attr(w.Wurfl, cattr, cvalue) != C.WURFL_OK {
err := C.wurfl_get_error_message(w.Wurfl)
return errors.New(C.GoString(err))
}
// now reload all important header in wurfl struct since they are different
// if the attr is WurflAttrExtraHeadersExperimental
if attr == WurflAttrExtraHeadersExperimental {
ihe := C.wurfl_get_important_header_enumerator(w.Wurfl)
w.ImportantHeaderNames = nil
// deallocate important headers C strings
for _, importantHeaderName := range w.importantHeaderCStringNames {
C.free(unsafe.Pointer(importantHeaderName))
}
w.importantHeaderCStringNames = nil
for i := 0; C.wurfl_important_header_enumerator_is_valid(ihe) != 0; i++ {
// get the header name
headerName := C.wurfl_important_header_enumerator_get_value(ihe)
// convert header name to go string
gheaderName := C.GoString(headerName)
// create a C string copy from the go string
cheaderName := C.CString(gheaderName)
// append to slice
w.ImportantHeaderNames = append(w.ImportantHeaderNames, gheaderName)
w.importantHeaderCStringNames = append(w.importantHeaderCStringNames, cheaderName)
// advance
C.wurfl_important_header_enumerator_move_next(ihe)
}
C.wurfl_important_header_enumerator_destroy(ihe)
}
return nil
}
// GetAttr : get engine attributes
func (w *Wurfl) GetAttr(attr int) (int, error) {
cattr := C.wurfl_attr(attr)
var cvalue C.int
if C.wurfl_get_attr(w.Wurfl, cattr, &cvalue) != C.WURFL_OK {
err := C.wurfl_get_error_message(w.Wurfl)
return 0, errors.New(C.GoString(err))
}
return int(cvalue), nil
}
// SetLogPath - set path of main libwurfl log file (updater has a separate log file)
func (w *Wurfl) SetLogPath(LogFile string) error {
// wurfl_error wurfl_set_log_path(wurfl_handle hwurfl, const char *log_path);
clog := C.CString(LogFile)
ret := C.wurfl_set_log_path(w.Wurfl, clog)
C.free(unsafe.Pointer(clog))
if ret != C.WURFL_OK {
err := C.wurfl_get_error_message(w.Wurfl)
return errors.New(C.GoString(err))
}
return nil
}
// SetUpdaterDataURL - set your scientiamobile WURFL Snapshot URL
func (w *Wurfl) SetUpdaterDataURL(DataURL string) error {
apiVersion := w.GetAPIVersion()
// we set useragent only if API version is >= 1.13.0.0 otherwise it will overwrite the libwurfl one
if CompareVersions(apiVersion, "1.13.0.0") >= 0 {
golangUA := "infuze_golang/" + Version
cgolangUA := C.CString(golangUA)
cret := C.wurfl_updater_set_useragent(w.Wurfl, cgolangUA)
C.free(unsafe.Pointer(cgolangUA))
if cret != C.WURFL_OK {
err := C.wurfl_get_error_message(w.Wurfl)
return errors.New(C.GoString(err))
}
}
cdata := C.CString(DataURL)
ret := C.wurfl_updater_set_data_url(w.Wurfl, cdata)
C.free(unsafe.Pointer(cdata))
if ret != C.WURFL_OK {
err := C.wurfl_get_error_message(w.Wurfl)
return errors.New(C.GoString(err))
}
return nil
}
// SetUpdaterUserAgent - set the UserAgent used in calling the WURFL Snapshot server
func (w *Wurfl) SetUpdaterUserAgent(userAgent string) error {
cdata := C.CString(userAgent)
ret := C.wurfl_updater_set_useragent(w.Wurfl, cdata)
C.free(unsafe.Pointer(cdata))
if ret != C.WURFL_OK {
err := C.wurfl_get_error_message(w.Wurfl)
return errors.New(C.GoString(err))
}
return nil
}
// GetUpdaterUserAgent - gets the UserAgent used in calling the WURFL Snapshot server
func (w *Wurfl) GetUpdaterUserAgent() string {
ua := C.wurfl_updater_get_useragent(w.Wurfl)
uaValue := C.GoString(ua)
return uaValue
}
// SetUpdaterDataFrequency - set frequency of update checks
func (w *Wurfl) SetUpdaterDataFrequency(Frequency int) error {
// LIBWURFLAPI wurfl_error wurfl_updater_set_data_frequency(wurfl_handle hwurfl, wurfl_updater_frequency freq);
cfreq := C.wurfl_updater_frequency(Frequency)
if C.wurfl_updater_set_data_frequency(w.Wurfl, cfreq) != C.WURFL_OK {
err := C.wurfl_get_error_message(w.Wurfl)
return errors.New(C.GoString(err))
}
return nil
}
// SetUpdaterDataURLTimeout - set connection and data transfer timeouts (in millisecs) for updater
// http call. 0 for no timeout, -1 for defaults
func (w *Wurfl) SetUpdaterDataURLTimeout(ConnectionTimeout int, DataTransferTimeout int) error {
// wurfl_error wurfl_updater_set_data_url_timeouts(wurfl_handle hwurfl, int connection_timeout, int data_transfer_timeout);
cConn := C.int(ConnectionTimeout)
cData := C.int(DataTransferTimeout)
if C.wurfl_updater_set_data_url_timeouts(w.Wurfl, cConn, cData) != C.WURFL_OK {
err := C.wurfl_get_error_message(w.Wurfl)
return errors.New(C.GoString(err))
}
return nil
}
// SetUpdaterLogPath - set path of updater log file
func (w *Wurfl) SetUpdaterLogPath(LogFile string) error {
// LIBWURFLAPI wurfl_error wurfl_updater_set_log_path(wurfl_handle hwurfl, const char* log_path);
clog := C.CString(LogFile)
ret := C.wurfl_updater_set_log_path(w.Wurfl, clog)
C.free(unsafe.Pointer(clog))
if ret != C.WURFL_OK {
err := C.wurfl_get_error_message(w.Wurfl)
return errors.New(C.GoString(err))
}
return nil
}
// UpdaterRunonce - Update the wurfl if needed and terminate
func (w *Wurfl) UpdaterRunonce() error {
// LIBWURFLAPI wurfl_error wurfl_updater_runonce(wurfl_handle hwurfl);
if C.wurfl_updater_runonce(w.Wurfl) != C.WURFL_OK {
err := C.wurfl_get_error_message(w.Wurfl)
return errors.New(C.GoString(err))
}
return nil
}
// UpdaterStart - Start the updater, a thread that performs periodic check and update of the wurfl.zip file
// when a new wurfl.zip is available it is downloaded and engine is switched to use the new wurfl.zip file immediately
func (w *Wurfl) UpdaterStart() error {
// LIBWURFLAPI wurfl_error wurfl_updater_start(wurfl_handle hwurfl);
if C.wurfl_updater_start(w.Wurfl) != C.WURFL_OK {
err := C.wurfl_get_error_message(w.Wurfl)
return errors.New(C.GoString(err))
}
return nil
}
// UpdaterStop - stop the updater
func (w *Wurfl) UpdaterStop() error {
if C.wurfl_updater_stop(w.Wurfl) != C.WURFL_OK {
err := C.wurfl_get_error_message(w.Wurfl)
return errors.New(C.GoString(err))
}
return nil
}
// GetAPIVersion returns version of internal InFuze API
func (w *Wurfl) GetAPIVersion() string {
return C.GoString(C.wurfl_get_api_version())
}
// GetAllVCaps return all virtual capabilities names
func (w *Wurfl) GetAllVCaps() []string {
var result []string
eh := C.wurfl_enum_create(w.Wurfl, WurflEnumVirtualCapabilities)
for C.wurfl_enum_is_valid(eh) != 0 {
cvcapname := C.wurfl_enum_get_name(eh)
vcapname := C.GoString(cvcapname)
result = append(result, vcapname)
C.wurfl_enum_move_next(eh)
}
C.wurfl_enum_destroy(eh)
return result
}
// GetAllCaps return all static capabilities names
func (w *Wurfl) GetAllCaps() []string {
var result []string
eh := C.wurfl_enum_create(w.Wurfl, WurflEnumStaticCapabilities)
for C.wurfl_enum_is_valid(eh) != 0 {
ccapname := C.wurfl_enum_get_name(eh)
capname := C.GoString(ccapname)
result = append(result, capname)
C.wurfl_enum_move_next(eh)
}
C.wurfl_enum_destroy(eh)
return result
}
// GetInfo - get wurfl.xml info
func (w *Wurfl) GetInfo() string {
return C.GoString(C.wurfl_get_wurfl_info(w.Wurfl))
}
// GetLastLoadTime - get last wurfl.xml load time
func (w *Wurfl) GetLastLoadTime() string {
return C.GoString(C.wurfl_get_last_load_time_as_string(w.Wurfl))
}
// GetLastUpdated - get last wurfl.xml update time
func (w *Wurfl) GetLastUpdated() string {
return C.GoString(C.wurfl_get_last_updated(w.Wurfl))
}
// GetEngineTarget - Returns a string representing the currently set WURFL Engine Target.
// DEPRECATED: will always return default value
func (w *Wurfl) GetEngineTarget() string {
return "DEFAULT"
}
// SetUserAgentPriority - Sets which UA wurfl is using
// DEPRECATED. Since 1.9.5.0 has no effect anymore
func (w *Wurfl) SetUserAgentPriority(prio int) {
return
}
// GetUserAgentPriority - Tells if WURFL is using the plain user agent or the sideloaded browser user agent for device detection
// DEPRECATED: will always return default value
func (w *Wurfl) GetUserAgentPriority() string {
return "OVERRIDE SIDELOADED BROWSER USERAGENT"
}
// HasCapability - returns true if the static capability exists in wurfl.zip
func (w *Wurfl) HasCapability(cap string) bool {
ccap := C.CString(cap)
ret := C.wurfl_has_capability(w.Wurfl, ccap)
C.free(unsafe.Pointer(ccap))
if ret == 0 {
return false
}
return true
}
// HasVirtualCapability - returns true if the virtual cap is available
func (w *Wurfl) HasVirtualCapability(vcap string) bool {
cvcap := C.CString(vcap)
ret := C.wurfl_has_virtual_capability(w.Wurfl, cvcap)
C.free(unsafe.Pointer(cvcap))
if ret == 0 {
return false
}
return true
}
// LookupDeviceID : lookup by wurfl_ID and return Device handle
func (w *Wurfl) LookupDeviceID(DeviceID string) (*Device, error) {
d := &Device{}
// copy wurfl handle into device handle for error handling
d.Wurfl = w.Wurfl
// copy the caps cache
d.capsCStringcache = w.capsCStringcache
wDeviceID := C.CString(DeviceID)
d.Device = C.wurfl_get_device(w.Wurfl, wDeviceID)
C.free(unsafe.Pointer(wDeviceID))
if d.Device == nil {
err := C.wurfl_get_error_message(w.Wurfl)
return nil, errors.New(C.GoString(err))
}
return d, nil
}
// LookupUserAgent : lookup up useragent and return Device handle
func (w *Wurfl) LookupUserAgent(ua string) (*Device, error) {
d := &Device{}
// copy wurfl handle into device handle for error handling
d.Wurfl = w.Wurfl
// copy the caps cache
d.capsCStringcache = w.capsCStringcache
wua := C.CString(ua)
d.Device = C.wurfl_lookup_useragent(w.Wurfl, wua)
C.free(unsafe.Pointer(wua))
if d.Device == nil {
err := C.wurfl_get_error_message(w.Wurfl)
return nil, errors.New(C.GoString(err))
}
return d, nil
}
// LookupRequest : Lookup using Request headers and return Device handle
func (w *Wurfl) LookupRequest(r *http.Request) (*Device, error) {
d := &Device{}
// copy wurfl handle into device handle for error handling
d.Wurfl = w.Wurfl
// copy the caps cache
d.capsCStringcache = w.capsCStringcache
// create important headers object to pass to lookup
cih := C.wurfl_important_header_create(w.Wurfl)
if cih == nil {
err := C.wurfl_get_error_message(w.Wurfl)
return nil, errors.New(C.GoString(err))
}
// use important header names loaded during create
for i, importantHeaderName := range w.ImportantHeaderNames {
// retrieve header value from passed request, if any
headerValue := r.Header.Get(importantHeaderName)
if len(headerValue) != 0 {
// create C strings from header value
cheaderValue := C.CString(headerValue)
// add this header to cih
// for header names we use a set of preallocated CStrings with headernames
C.wurfl_important_header_set(cih, w.importantHeaderCStringNames[i], cheaderValue)
C.free(unsafe.Pointer(cheaderValue))
}
}
d.Device = C.wurfl_lookup_with_important_header(w.Wurfl, cih)
C.wurfl_important_header_destroy(cih)
if d.Device == nil {
err := C.wurfl_get_error_message(w.Wurfl)
return nil, errors.New(C.GoString(err))
}
return d, nil
}
// LookupDeviceIDWithRequest : lookup by wurfl_ID and request headers and return Device handle
func (w *Wurfl) LookupDeviceIDWithRequest(DeviceID string, r *http.Request) (*Device, error) {
d := &Device{}
// copy wurfl handle into device handle for error handling
d.Wurfl = w.Wurfl
// copy the caps cache
d.capsCStringcache = w.capsCStringcache
wDeviceID := C.CString(DeviceID)
// create important headers object to pass to lookup
cih := C.wurfl_important_header_create(w.Wurfl)
if cih == nil {
err := C.wurfl_get_error_message(w.Wurfl)
return nil, errors.New(C.GoString(err))
}
// use important header names loaded during create
for i, importantHeaderName := range w.ImportantHeaderNames {
// retrieve header value from passed request, if any
headerValue := r.Header.Get(importantHeaderName)
if len(headerValue) != 0 {
// create C strings from header name and value
cheaderValue := C.CString(headerValue)
// add this header to cih
C.wurfl_important_header_set(cih, w.importantHeaderCStringNames[i], cheaderValue)
C.free(unsafe.Pointer(cheaderValue))
}
}
d.Device = C.wurfl_get_device_with_important_header(w.Wurfl, wDeviceID, cih)
C.wurfl_important_header_destroy(cih)
C.free(unsafe.Pointer(wDeviceID))
if d.Device == nil {
err := C.wurfl_get_error_message(w.Wurfl)
return nil, errors.New(C.GoString(err))
}
return d, nil
}
// LookupWithImportantHeaderMap : Lookup using header values found in IHMap.
// IHMap must be filled with Wurfl.ImportantHeaderNames and values
func (w *Wurfl) LookupWithImportantHeaderMap(IHMap map[string]string) (*Device, error) {
d := &Device{}
// copy wurfl handle into device handle for error handling
d.Wurfl = w.Wurfl
// copy the caps cache
d.capsCStringcache = w.capsCStringcache
// create important headers object to pass to lookup
cih := C.wurfl_important_header_create(w.Wurfl)
if cih == nil {
err := C.wurfl_get_error_message(w.Wurfl)
return nil, errors.New(C.GoString(err))
}
// fill it with IHMap entries
for importantHeaderName, headerValue := range IHMap {
// create C strings from header name and value
cheaderName := C.CString(importantHeaderName)
cheaderValue := C.CString(headerValue)
// add this header to WURFL importtant headers object
C.wurfl_important_header_set(cih, cheaderName, cheaderValue)
C.free(unsafe.Pointer(cheaderName))
C.free(unsafe.Pointer(cheaderValue))
}
d.Device = C.wurfl_lookup_with_important_header(w.Wurfl, cih)
C.wurfl_important_header_destroy(cih)
if d.Device == nil {
err := C.wurfl_get_error_message(w.Wurfl)
return nil, errors.New(C.GoString(err))
}
return d, nil
}
// LookupDeviceIDWithImportantHeaderMap : Lookup deviceID using header values found in IHMap.
// IHMap must be filled with Wurfl.ImportantHeaderNames and values
func (w *Wurfl) LookupDeviceIDWithImportantHeaderMap(DeviceID string, IHMap map[string]string) (*Device, error) {
d := &Device{}
// copy wurfl handle into device handle for error handling
d.Wurfl = w.Wurfl
// copy the caps cache
d.capsCStringcache = w.capsCStringcache
cDeviceID := C.CString(DeviceID)
// create important headers object to pass to lookup
cih := C.wurfl_important_header_create(w.Wurfl)
if cih == nil {
err := C.wurfl_get_error_message(w.Wurfl)
return nil, errors.New(C.GoString(err))
}
// fill it with IHMap entries
for i, importantHeaderName := range w.ImportantHeaderNames {
// retrieve header from IHMap
header := IHMap[importantHeaderName]
if len(header) != 0 {
// create a C string from header
cheader := C.CString(header)
// add this header to cih
C.wurfl_important_header_set(cih, w.importantHeaderCStringNames[i], cheader)
C.free(unsafe.Pointer(cheader))
}
}
d.Device = C.wurfl_get_device_with_important_header(w.Wurfl, cDeviceID, cih)
C.wurfl_important_header_destroy(cih)
C.free(unsafe.Pointer(cDeviceID))
if d.Device == nil {
err := C.wurfl_get_error_message(w.Wurfl)
return nil, errors.New(C.GoString(err))
}
return d, nil
}
// IsUserAgentFrozen : returns true if a UserAgent is frozen
func (w *Wurfl) IsUserAgentFrozen(ua string) bool {
wua := C.CString(ua)
ret := C.wurfl_is_ua_frozen(w.Wurfl, wua)
C.free(unsafe.Pointer(wua))
if ret == 0 {
return false
}
return true
}
// GetHeaderQuality returns an indicator of how many sec-ch-ua headers are present in the request
func (w *Wurfl) GetHeaderQuality(r *http.Request) (HeaderQuality, error) {
// create important headers object to pass to lookup
cih := C.wurfl_important_header_create(w.Wurfl)
if cih == nil {
err := C.wurfl_get_error_message(w.Wurfl)
return HeaderQualityNone, errors.New(C.GoString(err))
}
// use important header names loaded during create
for i, importantHeaderName := range w.ImportantHeaderNames {
// retrieve header value from passed request, if any
headerValue := r.Header.Get(importantHeaderName)
if len(headerValue) != 0 {
// create C strings from header name and value
cheaderValue := C.CString(headerValue)
// add this header to cih
C.wurfl_important_header_set(cih, w.importantHeaderCStringNames[i], cheaderValue)
C.free(unsafe.Pointer(cheaderValue))
}
}
hq := C.wurfl_important_header_uach_quality(cih)
C.wurfl_important_header_destroy(cih)
return HeaderQuality(hq), nil
}
/*
* Device methods
*/
// GetUserAgent Get default UserAgent of matched device (might be different from UA passed to lookup)
func (d *Device) GetUserAgent() (string, error) {
cua := C.wurfl_device_get_useragent(d.Device)
if cua == nil {
err := C.wurfl_get_error_message(d.Wurfl)
return "", errors.New(C.GoString(err))
}
ua := C.GoString(cua)
return ua, nil
}
// GetOriginalUserAgent Get the original userAgent of matched device (the one passed to lookup)
func (d *Device) GetOriginalUserAgent() (string, error) {
oua := C.wurfl_device_get_original_useragent(d.Device)
if oua == nil {
err := C.wurfl_get_error_message(d.Wurfl)
return "", errors.New(C.GoString(err))
}
ua := C.GoString(oua)
return ua, nil
}
// GetNormalizedUserAgent Get the Normalized (processed by wurfl api) userAgent ( Only for internal use/tooling)
func (d *Device) GetNormalizedUserAgent() (string, error) {
nua := C.wurfl_device_get_normalized_useragent(d.Device)
if nua == nil {
err := C.wurfl_get_error_message(d.Wurfl)
return "", errors.New(C.GoString(err))
}
ua := C.GoString(nua)
return ua, nil
}
// GetDeviceID Get wurfl_id string from device handle
func (d *Device) GetDeviceID() (string, error) {
cdeviceid := C.wurfl_device_get_id(d.Device)
if cdeviceid == nil {
err := C.wurfl_get_error_message(d.Wurfl)
return "", errors.New(C.GoString(err))
}
deviceid := C.GoString(cdeviceid)
return deviceid, nil
}
// GetRootID - Retrieve the root device id of this device.
func (d *Device) GetRootID() string {
return C.GoString(C.wurfl_device_get_root_id(d.Device))
}
// GetParentID - Retrieve the parent device id of this device.
func (d *Device) GetParentID() string {
return C.GoString(C.wurfl_device_get_parent_id(d.Device))
}
// IsRoot - true if device is device root
func (d *Device) IsRoot() bool {
if C.wurfl_device_is_actual_device_root(d.Device) == 0 {
return false
}
return true
}
// GetCapability Get a single Capability
// Deprecated: GetCapability is deprecated. Use GetStaticCap instead.
func (d *Device) GetCapability(cap string) string {
ccap, found := d.capsCStringcache[cap]
if !found {
// non existing capability?
ccap = C.CString(cap)
defer C.free(unsafe.Pointer(ccap))
}
ccapvalue := C.wurfl_device_get_capability(d.Device, ccap)
capvalue := C.GoString(ccapvalue)
return capvalue
}
// GetStaticCap Get a single static cap using new C.wurfl_device_get_static_cap()
// that returns error
func (d *Device) GetStaticCap(cap string) (string, error) {
ccap, found := d.capsCStringcache[cap]
if !found {
// non existing capability?
ccap = C.CString(cap)
defer C.free(unsafe.Pointer(ccap))
}
retCode := C.wurfl_error(0)
ccapvalue := C.wurfl_device_get_static_cap(d.Device, ccap, &retCode)
if retCode != C.WURFL_OK {
err := C.wurfl_get_error_message(d.Wurfl)
return "", errors.New(C.GoString(err))
}
capvalue := C.GoString(ccapvalue)
return capvalue, nil
}
// GetCapabilityAsInt gets a single static capability value that has a int type
// It returns an error if the requested static capability is not a numeric one (ie: brand_name)
func (d *Device) GetCapabilityAsInt(cap string) (int, error) {
ccap, found := d.capsCStringcache[cap]
if !found {
// non existing capability?
ccap = C.CString(cap)
C.free(unsafe.Pointer(ccap))
}
cErr := C.wurfl_error(0)
ccapvalue := C.wurfl_device_get_static_cap_as_int(d.Device, ccap, &cErr)
// libwurfl currently returns zero if any error occurs
if cErr != C.WURFL_OK {
err := C.wurfl_get_error_message(d.Wurfl)
return 0, errors.New(C.GoString(err))
}
return int(ccapvalue), nil
}
// GetCapabilities Get a list of Static Capabilities
// Deprecated: GetCapabilities is deprecated. Use GetStaticCaps instead.
func (d *Device) GetCapabilities(caps []string) map[string]string {
result := make(map[string]string, len(caps))
for i := 0; i < len(caps); i++ {
ccap, found := d.capsCStringcache[caps[i]]
if !found {
// non existing capability?
ccap = C.CString(caps[i])
defer C.free(unsafe.Pointer(ccap))
}
ccapvalue := C.wurfl_device_get_capability(d.Device, ccap)
capvalue := C.GoString(ccapvalue)
result[caps[i]] = capvalue
}
return result
}
// GetStaticCaps Get a list of Static Capabilities
func (d *Device) GetStaticCaps(caps []string) (map[string]string, error) {
var errMsg *C.char
result := make(map[string]string, len(caps))
for i := 0; i < len(caps); i++ {
ccap, found := d.capsCStringcache[caps[i]]
if !found {
// non existing capability?
ccap = C.CString(caps[i])
defer C.free(unsafe.Pointer(ccap))
}