github.com/letsencrypt/boulder@v0.20251208.0/test/chall-test-srv/httpone.go (about)

     1  package main
     2  
     3  import "net/http"
     4  
     5  // addHTTP01 handles an HTTP POST request to add a new HTTP-01 challenge
     6  // response for a given token.
     7  //
     8  // The POST body is expected to have two non-empty parameters:
     9  // "token" - the HTTP-01 challenge token to add the mock HTTP-01 response under
    10  // in the `/.well-known/acme-challenge/` path.
    11  //
    12  // "content" - the key authorization value to return in the HTTP response.
    13  //
    14  // A successful POST will write http.StatusOK to the client.
    15  func (srv *managementServer) addHTTP01(w http.ResponseWriter, r *http.Request) {
    16  	// Unmarshal the request body JSON as a request object
    17  	var request struct {
    18  		Token   string
    19  		Content string
    20  	}
    21  	if err := mustParsePOST(&request, r); err != nil {
    22  		http.Error(w, err.Error(), http.StatusBadRequest)
    23  		return
    24  	}
    25  
    26  	// If the request has an empty token or content it's a bad request
    27  	if request.Token == "" || request.Content == "" {
    28  		w.WriteHeader(http.StatusBadRequest)
    29  		return
    30  	}
    31  
    32  	// Add the HTTP-01 challenge to the challenge server
    33  	srv.challSrv.AddHTTPOneChallenge(request.Token, request.Content)
    34  	srv.log.Printf("Added HTTP-01 challenge for token %q - key auth %q\n",
    35  		request.Token, request.Content)
    36  	w.WriteHeader(http.StatusOK)
    37  }
    38  
    39  // delHTTP01 handles an HTTP POST request to delete an existing HTTP-01
    40  // challenge response for a given token.
    41  //
    42  // The POST body is expected to have one non-empty parameter:
    43  // "token" - the HTTP-01 challenge token to remove the mock HTTP-01 response
    44  // from.
    45  //
    46  // A successful POST will write http.StatusOK to the client.
    47  func (srv *managementServer) delHTTP01(w http.ResponseWriter, r *http.Request) {
    48  	// Unmarshal the request body JSON as a request object
    49  	var request struct {
    50  		Token string
    51  	}
    52  	if err := mustParsePOST(&request, r); err != nil {
    53  		http.Error(w, err.Error(), http.StatusBadRequest)
    54  		return
    55  	}
    56  
    57  	// If the request has an empty token it's a bad request
    58  	if request.Token == "" {
    59  		w.WriteHeader(http.StatusBadRequest)
    60  		return
    61  	}
    62  
    63  	// Delete the HTTP-01 challenge for the given token from the challenge server
    64  	srv.challSrv.DeleteHTTPOneChallenge(request.Token)
    65  	srv.log.Printf("Removed HTTP-01 challenge for token %q\n", request.Token)
    66  	w.WriteHeader(http.StatusOK)
    67  }
    68  
    69  // addHTTPRedirect handles an HTTP POST request to add a new 301 redirect to be
    70  // served for the given path to the given target URL.
    71  //
    72  // The POST body is expected to have two non-empty parameters:
    73  // "path" - the path that when matched in an HTTP request will return the
    74  // redirect.
    75  //
    76  // "targetURL" - the URL that the client will be redirected to when making HTTP
    77  // requests for the redirected path.
    78  //
    79  // A successful POST will write http.StatusOK to the client.
    80  func (srv *managementServer) addHTTPRedirect(w http.ResponseWriter, r *http.Request) {
    81  	// Unmarshal the request body JSON as a request object
    82  	var request struct {
    83  		Path      string
    84  		TargetURL string
    85  	}
    86  	if err := mustParsePOST(&request, r); err != nil {
    87  		http.Error(w, err.Error(), http.StatusBadRequest)
    88  		return
    89  	}
    90  
    91  	// If the request has an empty path or target URL it's a bad request
    92  	if request.Path == "" || request.TargetURL == "" {
    93  		w.WriteHeader(http.StatusBadRequest)
    94  		return
    95  	}
    96  	// Add the HTTP redirect to the challenge server
    97  	srv.challSrv.AddHTTPRedirect(request.Path, request.TargetURL)
    98  	srv.log.Printf("Added HTTP redirect for path %q to %q\n",
    99  		request.Path, request.TargetURL)
   100  	w.WriteHeader(http.StatusOK)
   101  }
   102  
   103  // delHTTPRedirect handles an HTTP POST request to delete an existing HTTP
   104  // redirect for a given path.
   105  //
   106  // The POST body is expected to have one non-empty parameter:
   107  // "path" - the path to remove a redirect for.
   108  //
   109  // A successful POST will write http.StatusOK to the client.
   110  func (srv *managementServer) delHTTPRedirect(w http.ResponseWriter, r *http.Request) {
   111  	// Unmarshal the request body JSON as a request object
   112  	var request struct {
   113  		Path string
   114  	}
   115  	if err := mustParsePOST(&request, r); err != nil {
   116  		http.Error(w, err.Error(), http.StatusBadRequest)
   117  		return
   118  	}
   119  
   120  	if request.Path == "" {
   121  		w.WriteHeader(http.StatusBadRequest)
   122  		return
   123  	}
   124  	// Delete the HTTP redirect for the given path from the challenge server
   125  	srv.challSrv.DeleteHTTPRedirect(request.Path)
   126  	srv.log.Printf("Removed HTTP redirect for path %q\n", request.Path)
   127  	w.WriteHeader(http.StatusOK)
   128  }