Transparent Modification of HTTP Requests

GolangGolangBeginner
Practice Now

Introduction

In this project, you will learn how to implement a transparent modification of HTTP requests using Go's http.RoundTripper interface. You will create a custom HTTP transport that calculates the MD5 hash of the request body and adds it to the request header as the "X-Md5" field.

👀 Preview

$ /usr/local/go/bin/go test
PASS
ok      md5transport    0.004s

ðŸŽŊ Tasks

In this project, you will learn:

  • How to encapsulate the http.RoundTripper interface to perform custom operations on HTTP requests and responses.
  • How to calculate the MD5 hash of the request body and add it to the request header.
  • How to reset the request body after calculating the MD5 hash to maintain the original request state.

🏆 Achievements

After completing this project, you will be able to:

  • Understand the concept of transparent modification of HTTP requests in Go.
  • Implement custom HTTP transport layers that can perform various operations on requests and responses.
  • Apply the knowledge to enhance your own applications with additional functionality, such as authentication, logging, or request/response manipulation.

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Golang`")) -.-> go/FunctionsandControlFlowGroup(["`Functions and Control Flow`"]) go(("`Golang`")) -.-> go/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) go(("`Golang`")) -.-> go/ErrorHandlingGroup(["`Error Handling`"]) go(("`Golang`")) -.-> go/FileOperationsGroup(["`File Operations`"]) go/FunctionsandControlFlowGroup -.-> go/if_else("`If Else`") go/ObjectOrientedProgrammingGroup -.-> go/methods("`Methods`") go/ErrorHandlingGroup -.-> go/errors("`Errors`") go/FileOperationsGroup -.-> go/reading_files("`Reading Files`") subgraph Lab Skills go/if_else -.-> lab-301260{{"`Transparent Modification of HTTP Requests`"}} go/methods -.-> lab-301260{{"`Transparent Modification of HTTP Requests`"}} go/errors -.-> lab-301260{{"`Transparent Modification of HTTP Requests`"}} go/reading_files -.-> lab-301260{{"`Transparent Modification of HTTP Requests`"}} end

Implement the MD5 Calculation and Header Modification

In this step, you will learn how to implement a simple data verification function that calculates the MD5 of the request body, converts it to hexadecimal, and adds it to the request header with the name X-Md5.

  1. Open the md5_transport.go file in the editor.
  2. Add the necessary content to import.
import (
	"bytes"
	"crypto/md5"
	"encoding/hex"
	"io"
	"net/http"
)
  1. In the RoundTrip function of the Transport struct, add the following code to calculate the MD5 of the request body and set it in the request header:
// Complete the code
// if reqeust body is not nil, calculate md5 and set to header
if req.Body != nil {
    body, err := io.ReadAll(req.Body)
    if err != nil {
        // If read body error, return the original request.
        // dont't interrupt the request
        return t.RoundTripper.RoundTrip(req)
    }

    // Calculate md5
    md5Hash := md5.Sum(body)
    md5HashStr := hex.EncodeToString(md5Hash[:])

    // Set md5 to header
    req.Header.Set("X-Md5", md5HashStr)

    // Reset request body
    req.Body = io.NopCloser(bytes.NewReader(body))
}

This code first checks if the request body is not nil. If it's not, it reads the entire body using io.ReadAll, calculates the MD5 hash using the md5 package, and converts the hash to a hexadecimal string. Then, it sets the "X-Md5" header with the calculated MD5 value. Finally, it resets the request body to the original state using bytes.NewReader.

  1. After adding the code, save the md5_transport.go file.

Test the Implementation

To test the implementation, follow these steps:

  1. Open the terminal in the editor.

  2. Navigate to the project directory:

    $ cd /home/labex/project
  3. Run the tests:

    $ /usr/local/go/bin/go test

If the code is implemented correctly, you should see the following output:

PASS
ok      md5transport    0.004s

This indicates that the tests have passed, and your implementation of the MD5 calculation and header modification is working as expected.

Understand the Transparent Modification of HTTP Requests

In Go, we often make network requests using http.Client, which internally creates an http.Request and then uses http.RoundTripper.RoundTrip to make the actual request. The real data transfer operations are performed inside RoundTrip.

By encapsulating the http.RoundTripper interface, we can perform various operations on the request and response, such as authentication, statistics, audit logging, and modifying requests, all in a transparent way to the user.

In this challenge, we've implemented a simple data verification function that calculates the MD5 of the request body and adds it to the request header. This is just one example of how you can use the http.RoundTripper interface to modify HTTP requests transparently.

When designing your module library or using someone else's components, you can think about how you can use this technique to add additional functionality or modify the behavior of your application in a transparent way.

âœĻ Check Solution and Practice

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Golang Tutorials you may like