github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/http/services/host-supported.go (about)

     1  /* For license and copyright information please see the LEGAL file in the code repository */
     2  
     3  package hs
     4  
     5  import (
     6  	"github.com/GeniusesGroup/libgo/detail"
     7  	"github.com/GeniusesGroup/libgo/http"
     8  	"github.com/GeniusesGroup/libgo/log"
     9  	"github.com/GeniusesGroup/libgo/mediatype"
    10  	"github.com/GeniusesGroup/libgo/protocol"
    11  	"github.com/GeniusesGroup/libgo/service"
    12  )
    13  
    14  var HostSupportedService = hostSupportedService{}
    15  
    16  func init() {
    17  	HostSupportedService.MT.Init("domain/http.protocol.service; name=host-supported")
    18  	HostSupportedService.DS.SetDetail(protocol.LanguageEnglish, domainEnglish,
    19  		"Host Supported",
    20  		"Service to check if requested host is valid or not",
    21  		"",
    22  		"",
    23  		nil)
    24  }
    25  
    26  type hostSupportedService struct {
    27  	detail.DS
    28  	mediatype.MT
    29  	service.Service
    30  }
    31  
    32  //libgo:impl protocol.MediaType
    33  func (s *hostSupportedService) FileExtension() string               { return "" }
    34  func (s *hostSupportedService) Status() protocol.SoftwareStatus     { return protocol.Software_PreAlpha }
    35  func (s *hostSupportedService) ReferenceURI() string                { return "" }
    36  func (s *hostSupportedService) IssueDate() protocol.Time            { return nil } // 1587282740
    37  func (s *hostSupportedService) ExpiryDate() protocol.Time           { return nil }
    38  func (s *hostSupportedService) ExpireInFavorOf() protocol.MediaType { return nil }
    39  func (s *hostSupportedService) Fields() []protocol.Field            { return nil }
    40  
    41  //libgo:impl protocol.Service
    42  func (s *hostSupportedService) URI() string                 { return "" }
    43  func (s *hostSupportedService) Priority() protocol.Priority { return protocol.Priority_Unset }
    44  func (s *hostSupportedService) Weight() protocol.Weight     { return protocol.Weight_Unset }
    45  func (s *hostSupportedService) CRUDType() protocol.CRUD     { return protocol.CRUD_All }
    46  func (s *hostSupportedService) UserType() protocol.UserType { return protocol.UserType_All }
    47  
    48  func (ser *hostSupportedService) ServeHTTP(stream protocol.Stream, httpReq *http.Request, httpRes *http.Response) (supported bool) {
    49  	var domainName = protocol.OS.AppManifest().DomainName()
    50  	var host = httpReq.URI().Host()
    51  	var path = httpReq.URI().Path()
    52  	var query = httpReq.URI().Query()
    53  
    54  	if host == "" {
    55  		// TODO::: noting to do or reject request??
    56  	} else if '0' <= host[0] && host[0] <= '9' {
    57  		// check of request send over IP
    58  		if protocol.AppMode_Dev {
    59  			protocol.App.Log(log.DeepDebugEvent(domainEnglish, "Host Check - IP host: "+host))
    60  		}
    61  
    62  		// TODO::: target alloc occur multiple, improve it.
    63  		var target = "https://" + domainName + path
    64  		if len(query) > 0 {
    65  			target += "?" + query // + "&rd=tls" // TODO::: add rd query for analysis purpose??
    66  		}
    67  		httpRes.SetStatus(http.StatusMovedPermanentlyCode, http.StatusMovedPermanentlyPhrase)
    68  		httpRes.H.Set(http.HeaderKeyLocation, target)
    69  		httpRes.H.Set(http.HeaderKeyCacheControl, "max-age=31536000, immutable")
    70  		return false
    71  	} else if len(host) > 4 && host[:4] == "www." {
    72  		if host[4:] != domainName {
    73  			if protocol.AppMode_Dev {
    74  				protocol.App.Log(log.DeepDebugEvent(domainEnglish, "Host Check - Unknown WWW 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  
    80  		if protocol.AppMode_Dev {
    81  			protocol.App.Log(log.DeepDebugEvent(domainEnglish, "Host Check - WWW host: "+host))
    82  		}
    83  
    84  		// Add www to domain. Just support http on www server app due to SE duplicate content both on www && non-www
    85  		// TODO::: target alloc occur multiple, improve it.
    86  		var target = "https://" + domainName + path
    87  		if len(query) > 0 {
    88  			target += "?" + query // + "&rd=tls" // TODO::: add rd query for analysis purpose??
    89  		}
    90  		httpRes.SetStatus(http.StatusMovedPermanentlyCode, http.StatusMovedPermanentlyPhrase)
    91  		httpRes.H.Set(http.HeaderKeyLocation, target)
    92  		httpRes.H.Set(http.HeaderKeyCacheControl, "max-age=31536000, immutable")
    93  		return false
    94  	} else if host != domainName {
    95  		if protocol.AppMode_Dev {
    96  			protocol.App.Log(log.DeepDebugEvent(domainEnglish, "Host Check - Unknown host: "+host))
    97  		}
    98  		// TODO::: Silently ignoring a request might not be a good idea and perhaps breaks the RFC's for HTTP.
    99  		return false
   100  	}
   101  	return true
   102  }
   103  
   104  func (ser *hostSupportedService) doHTTP(httpReq *http.Request, httpRes *http.Response) (err protocol.Error) {
   105  	return
   106  }