github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/http/service-host-supported.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package http
     4  
     5  import (
     6  	"../log"
     7  	"../mediatype"
     8  	"../protocol"
     9  	"../service"
    10  )
    11  
    12  var HostSupportedService = hostSupportedService{
    13  	Service: service.New("", mediatype.New("domain/http.protocol.service; name=host-supported").SetDetail(protocol.LanguageEnglish, domainEnglish,
    14  		"Host Supported",
    15  		"Service to check if requested host is valid or not",
    16  		"",
    17  		"",
    18  		nil).SetInfo(protocol.Software_PreAlpha, 1587282740, "")).
    19  		SetAuthorization(protocol.CRUDAll, protocol.UserType_All),
    20  }
    21  
    22  type hostSupportedService struct {
    23  	*service.Service
    24  }
    25  
    26  func (ser *hostSupportedService) ServeHTTP(stream protocol.Stream, httpReq *Request, httpRes *Response) (supported bool) {
    27  	var domainName = protocol.OS.AppManifest().DomainName()
    28  	var host = httpReq.uri.host
    29  	var path = httpReq.uri.path
    30  	var query = httpReq.uri.query
    31  
    32  	if host == "" {
    33  		// TODO::: noting to do or reject request??
    34  	} else if '0' <= host[0] && host[0] <= '9' {
    35  		// check of request send over IP
    36  		if protocol.LogMode_DeepDebug {
    37  			protocol.App.Log(log.DebugEvent(domainEnglish, "Host Check - IP host: "+host))
    38  		}
    39  
    40  		// TODO::: target alloc occur multiple, improve it.
    41  		var target = "https://" + domainName + path
    42  		if len(query) > 0 {
    43  			target += "?" + query // + "&rd=tls" // TODO::: add rd query for analysis purpose??
    44  		}
    45  		httpRes.SetStatus(StatusMovedPermanentlyCode, StatusMovedPermanentlyPhrase)
    46  		httpRes.H.Set(HeaderKeyLocation, target)
    47  		httpRes.H.Set(HeaderKeyCacheControl, "max-age=31536000, immutable")
    48  		return false
    49  	} else if len(host) > 4 && host[:4] == "www." {
    50  		if host[4:] != domainName {
    51  			if protocol.LogMode_DeepDebug {
    52  				protocol.App.Log(log.DebugEvent(domainEnglish, "Host Check - Unknown WWW host: "+host))
    53  			}
    54  			// TODO::: Silently ignoring a request might not be a good idea and perhaps breaks the RFC's for HTTP.
    55  			return false
    56  		}
    57  
    58  		if protocol.LogMode_DeepDebug {
    59  			protocol.App.Log(log.DebugEvent(domainEnglish, "Host Check - WWW host: "+host))
    60  		}
    61  
    62  		// Add www to domain. Just support http on www server app due to SE duplicate content both on www && non-www
    63  		// TODO::: target alloc occur multiple, improve it.
    64  		var target = "https://" + domainName + path
    65  		if len(query) > 0 {
    66  			target += "?" + query // + "&rd=tls" // TODO::: add rd query for analysis purpose??
    67  		}
    68  		httpRes.SetStatus(StatusMovedPermanentlyCode, StatusMovedPermanentlyPhrase)
    69  		httpRes.H.Set(HeaderKeyLocation, target)
    70  		httpRes.H.Set(HeaderKeyCacheControl, "max-age=31536000, immutable")
    71  		return false
    72  	} else if host != domainName {
    73  		if protocol.LogMode_DeepDebug {
    74  			protocol.App.Log(log.DebugEvent(domainEnglish, "Host Check - Unknown host: "+host))
    75  		}
    76  		// TODO::: Silently ignoring a request might not be a good idea and perhaps breaks the RFC's for HTTP.
    77  		return false
    78  	}
    79  	return true
    80  }
    81  
    82  func (ser *hostSupportedService) DoHTTP(httpReq *Request, httpRes *Response) (err protocol.Error) {
    83  	return
    84  }