github.com/bgentry/go@v0.0.0-20150121062915-6cf5a733d54d/src/mime/type_windows.go (about)

     1  // Copyright 2010 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package mime
     6  
     7  import (
     8  	"syscall"
     9  	"unsafe"
    10  )
    11  
    12  func initMime() {
    13  	var root syscall.Handle
    14  	rootpathp, _ := syscall.UTF16PtrFromString(`\`)
    15  	if syscall.RegOpenKeyEx(syscall.HKEY_CLASSES_ROOT, rootpathp,
    16  		0, syscall.KEY_READ, &root) != nil {
    17  		return
    18  	}
    19  	defer syscall.RegCloseKey(root)
    20  	var count uint32
    21  	if syscall.RegQueryInfoKey(root, nil, nil, nil, &count, nil, nil, nil, nil, nil, nil, nil) != nil {
    22  		return
    23  	}
    24  	var buf [1 << 10]uint16
    25  	for i := uint32(0); i < count; i++ {
    26  		n := uint32(len(buf))
    27  		if syscall.RegEnumKeyEx(root, i, &buf[0], &n, nil, nil, nil, nil) != nil {
    28  			continue
    29  		}
    30  		ext := syscall.UTF16ToString(buf[:])
    31  		if len(ext) < 2 || ext[0] != '.' { // looking for extensions only
    32  			continue
    33  		}
    34  		var h syscall.Handle
    35  		extpathp, _ := syscall.UTF16PtrFromString(`\` + ext)
    36  		if syscall.RegOpenKeyEx(
    37  			syscall.HKEY_CLASSES_ROOT, extpathp,
    38  			0, syscall.KEY_READ, &h) != nil {
    39  			continue
    40  		}
    41  		var typ uint32
    42  		n = uint32(len(buf) * 2) // api expects array of bytes, not uint16
    43  		contenttypep, _ := syscall.UTF16PtrFromString("Content Type")
    44  		if syscall.RegQueryValueEx(
    45  			h, contenttypep,
    46  			nil, &typ, (*byte)(unsafe.Pointer(&buf[0])), &n) != nil {
    47  			syscall.RegCloseKey(h)
    48  			continue
    49  		}
    50  		syscall.RegCloseKey(h)
    51  		if typ != syscall.REG_SZ { // null terminated strings only
    52  			continue
    53  		}
    54  		mimeType := syscall.UTF16ToString(buf[:])
    55  		setExtensionType(ext, mimeType)
    56  	}
    57  }
    58  
    59  func initMimeForTests() map[string]string {
    60  	return map[string]string{
    61  		".PnG": "image/png",
    62  	}
    63  }